Showing posts with label Fibonacci. Show all posts
Showing posts with label Fibonacci. Show all posts

Wednesday, May 16, 2018

C program to display Fibonacci series


C program to display Fibonacci series
Program code
#include <stdio.h>
#include <conio.h>
void main()
{
long int i, a1=0, a2=1, a3=1, n=1;
clrscr();
printf(“We are going to display a Fibonacci series.\nPlease input the number of terms you want to see in the Fibonacci series.\n”);
scanf(“%ld”, &n);
printf(“The Fibonacci series is displayed below:\n”);
if (n>=1)
printf(“%ld\,\t”,a1);
if (n>=2)
printf(“%ld\,\t”,a2);
if (n>2)
for (i=3;i<=n;i++)
{
 a3=a1+a2;
printf(“%ld\,\t”,a3);
a1=a2;
a2=a3;
}
getch();
}

Example of output
We are going to display a Fibonacci series.
Please input the number of terms you want to see in the Fibonacci series.
15
The Fibonacci series is displayed below:
0,         1,         1,         2,         3,         5,         8,         13,       21,       34,       55,       89,       144,           233,           377