C program to find Fibonacci series up to N terms | C Programs

 

//Fibonacci series
#include <stdio.h>
int main()
{
    int i=0, t1 = 0, t2 = 1, t3, n;
    printf("Enter the number of terms:: ");
    scanf("%d",&n);
    while(i < n)
    {
        printf("%d\n",t1);
        t3 = t1 + t2;
        t1 = t2;
        t2 = t3;
        i++;
    }
    return 0;
}

/*  At first 0 is printed 
    t3 = t1 + t2  -> t3 = 0 + 1 = 1
    t1 = t2 -> t1 = 1
    t2 = t3 -> t2 = 1
   Now 1 will be printed 
    t3 = t1 + t2  -> t3 = 1 + 1 = 2
    t1 = t2 -> t1 = 1
    t2 = t3 -> t2 = 2
  Now 1 will be printed 
   t3 = t1 + t2  -> t3 = 1 + 2 = 3
    t1 = t2 -> t1 = 2
    t2 = t3 -> t2 = 3
  Now 2 will be printed 
  0, 1, 1, 2… */

Output::

Enter the number of terms:: 7

0

1

1

2

3

5

8

Post a Comment

0 Comments