Tuesday, 5 September 2017

C-Program to print Fibonacci Series



#include<stdio.h>

int main()
{
 int a=0,b=1,num,c,count;
 printf("Enter a number to obtain fibonacci series\n");
 scanf("%d",&num);
 printf("The series is\n");
 printf("%d \n%d\n",a,b);
 count=2;
 while(count<num)
 {
   c=a+b;
   a=b;
   b=c;
   printf("%d\n",c);
   count++;
 }
return 0;
}

2 comments:

  1. Enter your comment... pls explain me i got confused

    ReplyDelete
  2. Initially Fibonacci series starts with 0 1 1 2 3 5 and so on

    expressed as xn = xn-1 + xn-2.

    A=0,B=1;

    Here initially we print 0 1 after that adding both 0&1 store in C variable. Print C value as 3rd Fibonacci

    number. After that swapping B value to

    A and new value obtained by (C=A+B) to be stored in B. this condition fails until count<given length.

    New value found is just swapped to B.before that old value of B is swapped to A.

    ReplyDelete