Ways to climb N stairs
Coding Problem Keys
Ways to climb N stairs
Problem Statement
There are N stairs to climb before reaching an office. The employee can either climb 1 stair or 2 stair at in a single step. The program must print the count of distinct ways for and employee to climb the N stairs.
Input Format
The first line will contain the value of N.
Output Format
The first line will contain the number of distinct ways to climb the N stair.
Boundary Condition(s)
1 <= N <= 99
Example Input/Output 1
Input
1
Output
1
Example Input/Output 2
Input
2
Output
2
Explanation
The steps can be 1, 1 or as 2.
Example Input/Output 3
Input
5
Output
8
Explanation
The steps can be
1, 1, 1, 1, 1 or
1, 1, 1, 2 or
1, 2, 2 or
1, 1, 2, 1 or
1, 2, 1, 1 or
2, 2, 1 or
2, 1, 2 or
2, 1, 1, 1.
Note: Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: Python 3 Language
n=int(input())
a=0;b=1;c=1
for i in range(n-1):
c=a+b
a=b
b=c
print(c+a)
# Published By PKJCODERS
Alter
n=int(input());a=1;b=0;result=0
if n<=0: print(0); exit()
if n==1: print(1); exit()
for i in range(1,n+1):
result=a+b
b=a
a=result
print(result)
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments