Fibonacci series
C Program to Display Fibonacci Sequence/Series.
The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2
In method 1, fibonacci series is generated upto 'n' number of terms, where n is entered by user
Whereas, in method 2, fibonacci series is generated upto a specific term. For example, if user enters 40, then, fibonacci series generated will be: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. Next term 55 is not generated as it is greater than 40.
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2
In method 1, fibonacci series is generated upto 'n' number of terms, where n is entered by user
Whereas, in method 2, fibonacci series is generated upto a specific term. For example, if user enters 40, then, fibonacci series generated will be: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. Next term 55 is not generated as it is greater than 40.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, t1, t2, next_term;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
t1 = 0;
t2 = 1;
for (i = 1; i <= n; i++)
{
printf("%d, ", t1);
next_term = t1 + t2;
t1 = t2;
t2 = next_term;
}
getch();
}
Output
Enter the number of terms: 8
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num, t1, t2, next_term;
printf("Enter the number upto which Fibonacci series is to be displayed\t:");
scanf("%d", &num);
printf("Fibonacci Series: 0, 1, ");
t1 = 0;
t2 = 1;
next_term = t1 + t2;
while(next_term <= num)
{
printf("%d, ", next_term);
t1 = t2;
t2 = next_term;
next_term = t1 + t2;
}
getch();
}
Output
Enter the number upto which Fibonacci series is to be displayed :190
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,