Search



Tuesday, 17 February 2015

Fibonacci

In this program, the Fibonacci series will be printed but the user will tell till when the series will be printed "n". for loop and if else functions together being used. Fibonacci series is the one in which the next no. is sum of previous two no.s . For eg : - 0,1,1,2,3,5,8,13,21 & so on.( 21 is sum of 13 & 8 similarly 8 is sum of 3& 5)

Program to print Fibonacci series.

#include<stdio.h>
#include<conio.h>
void main()
{
   int n,a=0,b=1,next,c;
   clrscr();
   printf("Enter the number of terms : ");
   scanf("%d",&n);
   printf("First %d terms of Fibonacci series are :\n",n);
   for (c=0 ; c<n; c++ )
   { if ( c <= 1 )
next = c;
else
{ next = a + b;
a = b;
b =  next;
}
      printf("%d\n",next);
   }
    getch();
}



No comments:

Post a Comment

Guys if you think something is wrong or should be edit than please do comment.