Count the number of ways to traverse a Matrix | Java Program for above approach ; Find factorial ; Find number of ways to reach mat [ m - 1 ] [ n - 1 ] from mat [ 0 ] [ 0 ] in a matrix mat [ ] [ ] ] ; Driver Code ; Function Call
import java . io . * ; class GFG { static int factorial ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } static int countWays ( int m , int n ) { m = m - 1 ; n = n - 1 ; return factorial ( m + n ) / ( factorial ( m ) * factorial ( n ) ) ; } public static void main ( String [ ] args ) { int m = 5 ; int n = 5 ; int result = countWays ( m , n ) ; System . out . println ( result ) ; } }