Wednesday 25 November 2015

Program to print Fibonacci series (0,1,1,2,3,5,8,....................n terms)

//Program to print Fibonacci series (0,1,1,2,3,5,8,....................n terms)
#include<stdio.h>
#include<conio.h>

void main()
{
      int a=0, b=1, c, i, n; /* 'a' and 'b' can be taken as input if the series has to be start from a certain point */
      clrscr(); // for clearing the console screen
      printf("Enter the number of terms to be printed: ");
      scanf("%d",&n);
      printf("\nThe series is: %d%d\n",a,b); // prints the initial values ,i.e., 0 and 1

// logic for the series (each term is the sum of its preceding two terms)

      for(i=0; i<n-2; i++) // here 'n-2' is taken because the initial two values are printed separately
      {
            c=a+b;
            printf("%d",c);
            a=b;
            b=c;
      } // end of i loop

      getch(); // for holding the console screen
}

No comments:

Post a Comment