MCPcopy Create free account
hub / github.com/chaharnishant11/CodeIn10DSA / nStair

Class nStair

Recursion/Code/NthStair.java:2–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import java.util.*;
2class 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected