Program to generate Fibonacci series up to n value The Fibonacci sequence of numbers “Fn” is defined using the recursive relation with the seed values F0=0 and F1=1: Fn = Fn-1+Fn-2
#include <stdio.h>
#include<conio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextterm;
nextterm = t1 + t2;
// get no. of terms from user
printf("Enter Number of Terms: ");
scanf("%d", &n);
// print the first two terms
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd onwards terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextterm);
t1 = t2;
t2 = nextterm;
nextterm = t1 + t2;
}
getch();
return 0;
}
Enter Number of Terms: 8
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,
--------------------------------
Process exited after 5.69 seconds with return value 0
Press any key to continue . . .