Saturday, September 17, 2016

Fibonacci Series in C language.


/* Fibonacci Series in C language.

About Fibonacci series: Each new number in a series is addition of its previous two numbers.
Example of Fibonacci series:0, 1, 1, 2, 3, 5, 8, 13,
*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
     int i, n, n1, n2, n3;
    
     printf("Enter your number:");
     scanf("%d",&n);
    
     if(n<2)
     {
           printf("Please enter number > 2. \n");
           exit(0);
     }

     n1=0; 
     n2=1;

     printf("First %d numbers of Fibonacci series:\n",n);
     printf("%d, %d, ", n1, n2);

     n3 = n1 + n2;
  
     for(i=0; i<n-2; i++)
     {
           printf("%d, ", n3);
           n1=n2;
           n2=n3;
           n3=n1+n2; 
     }
     return 0;
}


Output of the Program:
Enter your number:5
First 5 numbers of Fibonacci series:
0, 1, 1, 2, 3,


No comments:

Post a Comment