Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Program for Staircase Problem

Python Program for Staircase Problem

Staircase Problem

TCS CodeVita is a coding competition organized by TCS every year in search for some of the best coders all over the world. This year it is the ninth season of this competition known as TCS CodeVita 2020 season 9. Before the actual competition TCS CodeVita gives a set of sample question, Staircase Problem is one of the sample question of this year TCS CodeVita Season 9. Here we have solved this problem in Python.

Problem Description

There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time.

  • Count the number of ways, the person can reach the top.

Python Code

n = int(input("Enter number of Stairs:"))
def calc(x):
    if x <= 1:
        return x
    return calc(x - 1) + calc(x - 2)


def count(n):
    return calc(n + 1)


print("Number of ways:", count(n))
Output
5
8

Staircase Problem in Other Coding Languages

C

To find the solution of Staircase  problem in C Programming language click on the button below:

 

C

C++

To find the solution of Staircase  problem in C++ Programming language click on the button below:

 

C++

Java

To find the solution of Staircase  problem in Java Programming language click on the button below:

 

Java