# Number of ways to go from one point to another in a grid

Given the **NxN** grid of horizontal and vertical roads. The task is to find out the number of ways that the person can go from point A to point B using the shortest possible path.

**Note: ** A and B point are fixed i.e A is at top left corner and B at bottom right corner as shown in the below image.

In the above image, the path shown in the red and light green colour are the two possible paths to reach from point A to point B.

**Examples:**

Input:N = 3Output:Ways = 20Input:N = 4Output:Ways = 70

**Formula: **

Let the grid be N x N, number of ways can be written as.

**How does above formula work?**

Let consider the example of the 5×5 grid as shown above. In order to go from point A to point B in the 5×5 grid, We have to take 5 horizontal steps and 5 vertical steps. Each path will be an arrangement of 10 steps out of which 5 steps are identical of one kind and other 5 steps are identical of a second kind. Therefore

No. of ways = 10! / (5! * 5!) i.e 252 ways.

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function that will ` `// calculate the factorial ` `long` `factorial(` `int` `N) ` `{ ` ` ` `int` `result = 1; ` ` ` `while` `(N > 0) { ` ` ` `result = result * N; ` ` ` `N--; ` ` ` `} ` ` ` `return` `result; ` `} ` ` ` `long` `countWays(` `int` `N) ` `{ ` ` ` `long` `total = factorial(N + N); ` ` ` `long` `total1 = factorial(N); ` ` ` `return` `(total / total1) / total1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` `cout << ` `"Ways = "` `<< countWays(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `class` `GfG { ` ` ` ` ` `// function that will ` ` ` `// calculate the factorial ` ` ` `static` `long` `factorial(` `int` `N) ` ` ` `{ ` ` ` `int` `result = ` `1` `; ` ` ` `while` `(N > ` `0` `) { ` ` ` `result = result * N; ` ` ` `N--; ` ` ` `} ` ` ` `return` `result; ` ` ` `} ` ` ` ` ` `static` `long` `countWays(` `int` `N) ` ` ` `{ ` ` ` `long` `total = factorial(N + N); ` ` ` `long` `total1 = factorial(N); ` ` ` `return` `(total / total1) / total1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` `System.out.println(` `"Ways = "` `+ countWays(N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# function that will calculate the factorial ` `def` `factorial(N) : ` ` ` ` ` `result ` `=` `1` `; ` ` ` ` ` `while` `(N > ` `0` `) : ` ` ` ` ` `result ` `=` `result ` `*` `N; ` ` ` `N ` `-` `=` `1` `; ` ` ` ` ` `return` `result; ` ` ` `def` `countWays(N) : ` ` ` ` ` `total ` `=` `factorial(N ` `+` `N); ` ` ` `total1 ` `=` `factorial(N); ` ` ` ` ` `return` `(total ` `/` `/` `total1) ` `/` `/` `total1; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `5` `; ` ` ` ` ` `print` `(` `"Ways ="` `, countWays(N)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` `class` `GfG ` `{ ` ` ` ` ` `// function that will ` ` ` `// calculate the factorial ` ` ` `static` `long` `factorial(` `int` `N) ` ` ` `{ ` ` ` `int` `result = 1; ` ` ` `while` `(N > 0) ` ` ` `{ ` ` ` `result = result * N; ` ` ` `N--; ` ` ` `} ` ` ` `return` `result; ` ` ` `} ` ` ` ` ` `static` `long` `countWays(` `int` `N) ` ` ` `{ ` ` ` `long` `total = factorial(N + N); ` ` ` `long` `total1 = factorial(N); ` ` ` `return` `(total / total1) / total1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `int` `N = 5; ` ` ` `Console.WriteLine(` `"Ways = "` `+ countWays(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## PHP

0)

{

$result = $result * $N;

$N–;

}

return $result;

}

function countWays($N)

{

$total = factorial($N + $N);

$total1 = factorial($N);

return ($total / $total1) / $total1;

}

// Driver code

$N = 5;

echo “Ways = “, countWays($N);

// This code is contributed by ajit

?>

**Output:**

Ways = 252

## Recommended Posts:

- Ways to represent a number as a sum of 1's and 2's
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to pair people
- Number of ways to cut a stick of length N into K pieces
- Total number of ways to place X and Y at n places such that no two X are together
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to make mobile lock pattern
- Count ways to spell a number with repeated digits
- Number of ways to arrange a word such that all vowels occur together
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Number of ways to form a heap with n distinct integers
- Number of ways to select a node from each connected component
- Number of ways to arrange a word such that no vowels occur together
- Number of ways to reach Nth floor by taking at-most K leaps

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.