C Program to Print Fibonacci Numbers

Before looking into the program to print Fibonacci numbers, let us look into what really it is.  Fibonacci numbers are a sequence of numbers known as a Fibonacci sequence which is characterized by the fact that ‘every number after the first two is the sum of the two preceding ones’ as shown below:

1, 1, 2, 3, 5, 8, 13, 21, …

From the sequence given above, we can see that there exists a recurrence relation as:

F(n) = F(n-1)+F(n-2)

Let us use this relation to print the Fibonacci numbers, using a recursive method.

#include<stdio.h>
int FIB(int);
main()
{
int n, i, c=0;
printf("\nEnter the number of terms:");
scanf("%d", &amp;n);
for(i=0; i&lt;n; i++)
{
printf("%d ", FIB(i));
c++;
}
return 0;
}
int FIB(int x)
{
if(x==0)
return 0;
else if(x==1)
return 1;
else
return(FIB(x-1)+FIB(x-2));
}
Code language: PHP (php)

Discover more from BHUTAN IO

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top