C program to display the Fibonacci Series
Explanation
The Fibonacci series are integer sequence of 0,1,1,2,3,5,8…..The first two numbers in the series are 0 and 1.The subsequent numbers are generates by adding the two previous numbers.
Solution
Example : 0,1,1,2,3,5,8…
first number = 0
second number = 1
third number = first number + second number => 1
Fourth number = third number + second number =>2
Fibo(n) = Fibo(n-1) + Fibo(n-2)
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<stdio.h> int main() { int first=0,second=1,next,n,i; printf("Enter the number of terms for Fibonacci Series:\n"); scanf("%d",&n); printf("The first %d terms of Fibonacci Series are:\n",n); for(i=0;i<n;i++) { if(i<=1) { next=i; } else { next = first + second; first = second; second = next; } printf("%d\n",next); } return 0; } |