Thursday, 21 August 2014

Filled Under: ,

C Program to generate the Fibonacci Series

Share

Fibonacci series is a sequence of numbers in which every number is equal to the sum of  previous two numbers.
Here a program is given below which take the first two number and the number of terms as input  and print a series on output screen.

C program to generate fibonacci series

#include<stdio.h>
int main()
{
int first, second, sum, num, counter = 0;
printf("Enter the term : ");
scanf("%d", &num);
printf("nEnter First Number : ");
scanf("%d", &first);
printf("nEnter Second Number : ");
scanf("%d", &second);
printf("nFibonacci Series : %d %d ", first, second);
while (counter < num) 
{
  sum = first + second;
  printf("%d ", sum);
  first = second;
  second = sum;
  counter++;
}
return (0);
}
 Output:
Enter the term : 5
Enter First Number : 1
Enter Second Number :2
Fibonacci Series : 1 2 3 5 8