Write a C program to print Fibonacci series

Today, we are writing a C program to print Fibonacci series using for loop. This is a simple loop program where user enter’s the value of ‘last term’ and we will display Fibonacci series from 0.

e.g. if a user enters last term value =5 so Fibonacci series will be 0,1,1,2,3

What is Fibonacci Series

The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..etc

c program to print fibonacci series
Fibonacci series

In the above series, we add the first two values and make the third value as a result. e.g.

a=0 and b=1 
so c= a+b = 0+1=1
next time, we take a=1 and b=1
so c=a+b=1+1=2
similarly, next time a=1 and b=2
so c=a+b=1+2=3 and so on...

Let’s start the C program to print Fibonacci series

#include<stdio.h>
#include<conio.h>

void main()
{
int term, a,b, sum=0;
clrscr();

printf("Enter number of terms");
scanf("%d",&term);

a=0;
b=1;
printf("Fibonacci Series");
for(i=1;i<=term;++i)
{
printf("%d,",a);
sum=a+b;
a=b;
b=sum;
}
getch();
}

Output

Enter number of terms: 10
Fibonacci Series 0,1,1,2,3,5,8,13,21,34

Program Code Explanation (PCE)

I have already discussed above what is Fibonacci series and you can see above program code. It will print the total terms from 0. You need two initial variables to store 0 and 1 respectively (like a=0,b=1).

Then we initialize a new variable ‘i’ from 1 and start a loop from 1 to ‘term’ variable. And print the value of a. Then add both variables (a,b) into ‘sum’ variable. After adding swap the values (a=b and b=sum).

It will display a Fibonacci series.

Hope that you understand the C program to print Fibonacci series. Please ask your questions in below comment section.

Other programs

You can also visit our tech blog https://zerootech.com

Leave a Comment