Write a C Program to generate the first n terms of the Fibonacci series.
#include<stdio.h>
void main()
{
int f,s,t,n,i; //’f’ is 1st num,’s’ is 2nd num,’t’ for 3rd new generated num, ‘n’ is no.of terms, ‘i’ is for iterations.
printf(“Enter a value : “);
scanf(“%d”,&n); // Read the number terms to print from the series.
if(n<=0)
{
printf(“Please enter a valid number\n”);
return;
}
f=0;
printf(“The FIBONACCI Series is :\n%d”,f);
if(n>1)
{
s=1;
printf(“\t%d”,s);
}
else
return;
for(i=2;i<n;i++)
{
t=f+s;//By adding of preceding numbers next numbers will generate in Fibonacci series.
printf(“\t%d”,t);
f=s;
s=t;//Shifting the last two terms of series to ‘f’ & ‘s’ for generating next number in the series.
}
return;
}
Output:
i) Enter a value : 16
The FIBONACCI Series is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
ii) Enter a value : 0
Please enter a valid number
For improvement of your coding skills we give some tasks along this. If you solve this tasks and send to our email (onlineexamshubteam@gmail.com) with your details. We will display your details(like Name, City, college, photo) in our site.
Task: Write a C program to generate the given range of terms of Fibonacci series.
For example user entered 7 & 10 then output will be: 8 13 21 34