- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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 int answer = 0; // our answer answer = fact(n+n); // finding factorial of 2*n answer = answer / (fact(n) * fact(n)); // (2*n)! / (n! + n!) cout << answer << "\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.

- Related Questions & Answers
- Program to find number of ways we can reach from top left point to bottom right point in Python
- C++ Program to find out the moves to read a point from another point in a 2D plane
- C++ program to find out the number of ways a grid with boards can be colored
- Find the Number of Ways to Pair People using C++
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- C++ program to find out number of changes required to get from one end to other end in a grid
- Number of Ways to Paint N × 3 Grid in C++
- Moving a file from one directory to another using Java
- Finding reflection of a point relative to another point in JavaScript
- Program to find minimum number of operations required to make one number to another in Python
- Program to find number of good ways to split a string using Python
- Number of Ways to Paint N × 3 Grid in C++ program
- C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
- How to move a file from one folder to another using Python?
- Send data from one Fragment to another using Kotlin?