
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count ways to reach the nth stair using step 1, 2 or 3 in C++
We are given a total number of steps in a staircase that is n. A person can reach the next floor by skipping 1, 2 or 3 steps at a time. The goal is to find the number of ways in which the next floor can be reached by doing so.
We will use the recursive method by keeping in mind that to reach any i’th step, a person has to jump from i-1th step ( skip 1 step) , i-2th step (skip 2 steps ) or i-3th step ( skip 3 steps ).
Let’s understand with examples.
Input
N=3 steps
Output
Count of ways to reach the nth stair using step 1, 2 or 3 are: 4
Explanation
There are total 3 steps Jump from start ( skip 3 ) : 3 step Jump from 1’st step (skip 2): 1+2 Jump from 2nd step (skip 1): 2+1 No skip 1+1+1
Input
N=6 steps
Output
Count of ways to reach the nth stair using step 1, 2 or 3 are: 24
Explanation
There are total 6 steps Ways: 1+1+1+1+1+1, 2+1+1+1+1, 3+1+1+1, 3+1+2, 3+2+1, 3+3 and so on.
Approach used in the below program is as follows
We are taking integer steps as a total number of steps.
Function stairs_step(int steps) takes all steps as input and returns a number of ways to reach the next floor by taking jumps or not..
Take the initial variable count as 0 for such ways.
If the number is 0 return 1.
If step count is 1 only 1 way.
If step count is 2 only 2 ways ( 1+1 or 2 ).
Else the ways = stairs_step (step-3)+stair_step(step-2)+stair_step(step-1).
(recursive method)
Example
#include <iostream> using namespace std; int stairs_step(int steps){ if(steps == 0){ return 1; } else if(steps == 1){ return 1; } else if (steps == 2){ return 2; } else{ return stairs_step(steps - 3) + stairs_step(steps - 2) + stairs_step(steps - 1); } } int main(){ int steps = 5; cout<<"Count of ways to reach the nth stair using step 1, 2 or 3 are: "<<stairs_step(steps); return 0; }
Output
If we run the above code it will generate the following output −
Count of ways to reach the nth stair using step 1, 2 or 3 are: 13
- Related Articles
- Count ways to reach the n’th stair
- Count ways to reach a score using 1 and 2 with no consecutive 2s in C++
- Count number of ways to jump to reach end in C++
- Count number of ways to reach destination in a Maze in C++
- Count number of ways to reach a given score in a game
- Count number of ways to reach a given score in a Matrix in C++
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. in C++
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- A water tank has steps inside it. A monkey is sitting on the topmost step (i.e. the first step). The water level is at the ninth step:(i) He jumps 2 steps down and then jumps back 1 step up. In how many jumps will he reach the water level?(ii) After drinking water, he want to go back. For this, he jumps 3 steps up and 1 step down in every move. In how many jumps will he reach the top level?
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
- Step Count Method in Algorithm
- Observe the following pattern:$1^{3}=1$$1^{3}+2^{3}=(1+2)^{2}$$1^{3}+2^{3}+3^{3}=(1+2+3)^{2}$Write the next three rows and calculate the value of $1^3 + 2^3 + 3^3 +…. + 9^3 + 10^3$ by the above pattern.
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- Program to find number of ways we can reach to the next floor using stairs in Python
