| 1 | import java.util.*; |
| 2 | class nStair |
| 3 | { |
| 4 | |
| 5 | static int countWays(int n) |
| 6 | { |
| 7 | if(n==1 || n==2) // base condition; |
| 8 | return n; |
| 9 | |
| 10 | int recResult1 = countWays(n-1); |
| 11 | int recResult2 = countWays(n-2); |
| 12 | |
| 13 | int smallCal = recResult1+recResult2; |
| 14 | |
| 15 | return smallCal; |
| 16 | |
| 17 | |
| 18 | //return (n == 1 || n == 2) ? n : countWays(n-1) + countWays(n-2); //--One liner using ternary operator. |
| 19 | } |
| 20 | |
| 21 | // Driver Code |
| 22 | public static void main(String args[]) |
| 23 | { |
| 24 | Scanner in = new Scanner(System.in); |
| 25 | System.out.print("Enter a number : "); |
| 26 | int n = in.nextInt(); |
| 27 | |
| 28 | System.out.println("Number of ways to reach " + n + "th step is " + countWays(n)); |
| 29 | } |
| 30 | } |
nothing calls this directly
no outgoing calls
no test coverage detected