Count the number of ways to reach Nth stair by taking jumps of 1 to N - GeeksforGeeks

10 Jan, 2022
Given an integer N which depicts the number of stairs, the task is to count the number of ways to reach Nth stair by taking jumps of 1 to N.
Examples: 
Input: N = 2Output: 2Explanation: Two ways to reach are: (1, 1) & (2)
Input: N = 3Output: 4
Input: N = 4Output: 8
Approach: In this problem, the number of ways to reach the ith stair is:
Ways to reach ith stair = (Sum of ways to reach stairs 1 to i-1)+1As for any stair before i, ith stair can be reached in a single jump. And +1 for jumping directly to i.
Now to solve this problem: 
Create a variable sum to store the number of ways to reach on a particular stair. Initialise it with 0.Run a loop from i=1 to i=N-1 and for each iteration:Create a variable, say cur to store the number of ways to the current stair. So, cur = sum + 1.Change sum to sum = sum + cur.Return sum + 1 after the loop ends as the answer to this problem.
Create a variable sum to store the number of ways to reach on a particular stair. Initialise it with 0.
Run a loop from i=1 to i=N-1 and for each iteration:Create a variable, say cur to store the number of ways to the current stair. So, cur = sum + 1.Change sum to sum = sum + cur.
Create a variable, say cur to store the number of ways to the current stair. So, cur = sum + 1.Change sum to sum = sum + cur.
Create a variable, say cur to store the number of ways to the current stair. So, cur = sum + 1.
Change sum to sum = sum + cur.
Return sum + 1 after the loop ends as the answer to this problem.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ code for the above approach #include <bits/stdc++.h>using namespace std; // Function to count the number of ways// to reach Nth stairint findWays(int N){     int sum = 0;    for (int i = 1; i < N; i++) {        int cur = sum + 1;        sum += cur;    }     return sum + 1;} // Driver Codeint main(){    int N = 10;    cout << findWays(N);}
// Java code for the above approachimport java.util.*;public class GFG{   // Function to count the number of ways  // to reach Nth stair  static int findWays(int N)  {     int sum = 0;    for (int i = 1; i < N; i++) {      int cur = sum + 1;      sum += cur;    }     return sum + 1;  }   // Driver Code  public static void main(String args[])  {    int N = 10;    System.out.print(findWays(N));  }} // This code is contributed by Samim Hossain Mondal.
# python3 code for the above approach # Function to count the number of ways# to reach Nth stairdef findWays(N):     sum = 0    for i in range(1, N):        cur = sum + 1        sum += cur     return sum + 1 # Driver Codeif __name__ == "__main__":     N = 10    print(findWays(N)) # This code is contributed by rakeshsahni
// C# code to implement above approachusing System;class GFG{   // Function to count the number of ways  // to reach Nth stair  static int findWays(int N)  {     int sum = 0;    for (int i = 1; i < N; i++) {      int cur = sum + 1;      sum += cur;    }     return sum + 1;  }   // Driver code  public static void Main()  {    int N = 10;    Console.Write(findWays(N));  }} // This code is contributed by Samim Hossain Mondal.
<script>      // JavaScript code for the above approach       // Function to count the number of ways      // to reach Nth stair      function findWays(N) {           let sum = 0;          for (let i = 1; i < N; i++) {              let cur = sum + 1;              sum += cur;          }           return sum + 1;      }       // Driver Code       let N = 10;      document.write(findWays(N)); // This code is contributed by Potta Lokesh  </script>
  
512
 
Time Complexity: O(N)Auxiliary Space: O(1)
 
lokeshpotta20
rakeshsahni
samim2000
Combinatorial
Mathematical
Pattern Searching
Mathematical
Combinatorial
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Combinations with repetitions
Number of handshakes such that a person shakes hands only once
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Generate all possible combinations of at most X characters from a given array
Largest substring with same Characters
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Program to find GCD or HCF of two numbers