# Find the Number of Ways to go From One Point to Another in a Grid using C++

C++Server Side ProgrammingProgramming

In this article, we are given a question in which we need to find the total number of ways from point A to B where A and B are fixed points, i.e., A is the top-left point in the grid and B is the bottom right point in the grid for example −

Input : N = 5
Output : 252

Input : N = 4
Output : 70

Input : N = 3
Output : 20

In the given problem, we can formalize the answer by simple observations and get our results.

## Approach to find The Solution

In this approach, we make up a formula for the given problem by the observations that for traveling through the grid from A to B, we are required to travel n times in right direction and n times in downward direction, so that means we need to find all the possibilities of combination of these paths, so that gives us the formula of the combination of (n+n) and n.

## Example

#include<bits/stdc++.h>

using namespace std;
int fact(int n){ // factorial function
if(n <= 1)
return 1;
return n * fact(n-1);
}
int main() {
int n = 5; // given n
answer = fact(n+n); // finding factorial of 2*n
answer = answer / (fact(n) * fact(n)); // (2*n)! / (n! + n!)
}

## Output

252

## Explanation of the above code

In this code, we calculate the formula of a combination of 2*n to n as we know that for traveling from point A to B, we are going to require exactly 2*n operations in two directions, i.e., n operation in one direction and n operation in other and hence we find all the possible combinations of these operations, i.e. (2*n)!/ (n! + n!). The overall time complexity of the given program is O(1) which means that our complexity doesn’t depend on the given n.

## Conclusion

In this article, we discussed a problem to find the number of ways to go from one point to another in a grid. We also learned the C++ program for this problem and the complete approach we solved. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.