Java: Find the distinct ways you can climb to the top
Distinct Ways to Climb Stairs
Write a Java program to find distinct ways to climb to the top (n steps to reach the top) of stairs. Each time you climb, you can climb 1 or 2 steps.
Example: n = 5 a) 1+1+1+1+1 = 5 b) 1+1+1+2 = 5 c) 1+2+2 = 5 d) 2+2+1 = 5 e) 2+1+1+1 = 5 f) 2+1+2 = 5 g) 1+2+1+1 = 5 h) 1+1+2+1 = 5
Pictorial Presentation:
Sample Solution:
Java Code:
Sample Output:
Distinct ways can you climb to the top: 8
Flowchart:
For more Practice: Solve these Related Problems:
- Modify the program to allow steps of 1, 2, or 3.
- Write a program to find the number of ways with a memoized approach.
- Modify the program to print all distinct ways.
- Write a program to count the ways using an iterative approach.
Go to:
PREV : Min Path Sum in Grid.
NEXT : Remove Duplicates in Linked List.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.