C Program to generate first n Fibonacci terms
Fibonacci series or sequence is the series of numbers given as:
0, 1, 1, 2, 3, 5, 8, 13, 21 and so on
Fibonacci series or sequence starts with two numbers 0 and 1, and next number is obtained by adding two numbers before it. For example, third term 1 is found by adding 0 and 1, fourth term 2 is then obtained by third term 1 and second term 1.
First term: 0
Second term: 1
Third term: Second term + First Term = 0+1 = 1
Fourth term: Third term + Second term = 1+1 = 2
Fifth term: Fourth term + Third term = 2+1 = 3
Sixth term: Fifth term + Fourth term = 3+2 = 5
and so on...
Fibonacci upto nth term C program
#include<stdio.h>
#include<conio.h>
int main()
{
int n, a=0, b=1, next, i;
clrscr();
printf("Enter how many terms? ");
scanf("%d", &n);
printf("First %d Fibonacci terms are: \n", n);
for(i=1;i<=n;i++)
{
printf("%d\t", a);
next = a + b;
a = b;
b = next;
}
getch();
return(0);
}
Output of the above program :
Enter how many terms? 8 ↲ First 8 Fibonacci terms are: 0 1 1 2 3 5 8 13 Note: ↲ indicates enter is pressed.