C Program for Staircase Problem (TCS Codevita) | PrepInsta

C program for staircase problem

Staircase Problem

Staircase Problem is one of the sample problem of this year TCS CodeVita 2020 Season 9 coding competition. This is a prestigious coding competition that is organized by TCS every year for promotion Programming-As-A-Sport among the new generation. This competition’s difficulty level is pretty high comparing with other coding competitions.

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.

C Code

#include  <stdio.h>
int calc(int n);  
int count(int x);
 
int main ()
{
  int n ;
  printf("Enter number of stairs : ");
  scanf("%d", &n);
  printf("Number of ways = %d", count(n));
  getchar();
  return 0;
}
int count(int x)
{
	return calc(x + 1);
}
int calc(int n)
{
   if (n <= 1)
  	return n;
   return calc(n-1) + calc(n-2);
}
Output
Enter number of stairs : 5
Number of ways = 8

Staircase Problem in Other Coding Languages

Python

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

 

Python

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

One comment on “C Program for Staircase Problem (TCS Codevita) | PrepInsta”


  • mevinodrao

    anotherway in c language
    #include
    int i,j,k,x,y,z,a,b,c,sum=0,d,e,f;
    unsigned int factorial(int);
    int main()
    {
    printf(“enter the number of steps”);
    scanf(“%d”,&n);
    a=n/2;
    for(i=1;i<=a;i++)
    {
    x=((n-2*i)+i);
    y=n-2*i;
    z=i;
    d=factorial(x);
    e=factorial(y);
    f=factorial(z);
    sum=sum+(d/(e*f));
    }
    printf("the number of ways is %d\n",sum+1);
    return 0;
    }

    unsigned int factorial(int y)
    {
    if (y == 0)
    return 1;
    return y * factorial(y – 1);
    }