find the number ways in which Jimmy can reach the top of N stairs.
1.He can jump from one step to the next consucative step. 2.He can jump from one step tk another step leaving a step in between.
Input:4(value of N)
Output:5
Jimmy Jumps
1 import java.io.BufferedReader;
2 import java.io.InputStreamReader;
3
5 class Main {
6 public static void main(String args[] ) throws Exception {
7
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = 4;// read the name from the input
10
String input_from_question = br.readLine();
BU BEBoa
է
12
//Write your answer here
13
// print the Output
System.out.println(input_from_question);
15
}
18 }
Answers
Answered by
1
Answer:
Input: n = 1
Output: 1
There is only one way to climb 1 stair
Input: n = 2
Output: 2
There are two ways: (1, 1) and (2)
Input: n = 4
Output: 5
(1, 1, 1, 1), (1, 1, 2), (2, 1, 1), (1,
Answered by
3
Answer:
output 5 (in c language)
Explanation:
int totalWays(int n)
{
// base case
if (n < 0)
return 0;
// base case: there is one way to cover it with no steps
if (n == 0)
return 1;
// combine results of taking 3 step or 2 steps or 1 steps at a time
return totalWays(n - 2)+totalWays(n-1);
}
int main(void)
{
int n = 4;
printf("Total ways to reach the %d'th stair are %d", n, totalWays(n));
return 0;
}
Similar questions