Challenge: Climbing Stairs
Completed
Climbing 1 or 2 steps at a time, print the number of ways to climb n steps.
Input
One whole number n (1 ≤ n ≤ 90).
Output
The number of ways.
Example
Input
2
Output
2
Editor
C++
Hints
ways[i] = ways[i - 1] + ways[i - 2].
Use long long: the answer for 90 is about 4.6 × 10¹⁸.
Solution explanation
Fibonacci in disguise, solved bottom-up in O(n).
Show solution
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
long long a = 1, b = 1;
for (int i = 2; i <= n; i++) {
long long c = a + b;
a = b;
b = c;
}
cout << b << endl;
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module