Count the total number of ways to reach the Nth stair | faceprep

Count the total number of ways to reach the Nth stair | faceprep

The program to count the total number of ways to reach the Nth stair is discussed here.

Count the total number of ways to reach the Nth stair


DESCRIPTION:

There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time.

Count the total number of ways, the person can reach the top.

Sample Input:

4 (Number of stairs)

Sample Output:

5 (Number of ways)

Explanation:

Way 1: 1-1-1-1-1

Way 2: 1-2-1

Way 3: 2-1-1

Way 4: 1-1-2

Way 5: 2-2


face prep pro ad bannerClick here to learn more about FACE Prep PRO


Algorithm to count the total number of ways to reach the Nth stair

  • Input the total number of stairs.
  • This problem is clearly recursive.
  • The person can reach the Nth stair from the (N-1)th stair or (N-2)th stair.
  • Recursively call, ways(n) = ways(n-1) + ways(n – 2)


Program to count the total number of ways to reach the Nth stair

@@coding::1@@


Count the total number of ways to reach the Nth stair

c