Stair case problem

 

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.
 Coding in c:

#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 : 7
Number of ways = 21











Comments