Printing Series 2, 1, 3, 4, 7, 11, 18 up to n terms

Problem Statement
Write a program to display the following series:
    2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123.nth term.
Take n as user input. The first two terms of the series are 2 and 1 respectively and remaining terms are the sum of their previous two terms in the series e.g.,
     3 = 1+2
    4 = 3+1


#include<stdio.h>
int main()
{
  int n,low,high,curr,index=3;
  printf("enter the number of turms to be printed\n");
  scanf("%d",&n);
  printf("2 1 ");
  low=2;
  high=1;
  while(index<=n)
    {
      curr=low+high;
      low=high;
      high=curr;
      printf("%d ",curr);
      index++;
    }
  return 0;
}

No comments:

Post a Comment