Write a program which checks a given integer is Fibonacci number or not.
#include<stdio.h>
void main()
{
int f,s,t,n,i,flg; //’f’ is 1st num,’s’ is 2nd num,’t’ for 3rd new generated num, ‘n’ is user defined, ‘i’ is for iterations, “flg” is Flag variable.
printf(“Enter a number : “);
scanf(“%d”,&n); //Read the number.
if(n<0)
{
printf(“Please enter a valid number\n”);
return;
}
f=flg=t=0; //set f,flg and t variables set as zero for check conditions.
if(n==0)
flg=1; //set flg variable as ‘1’ for founding the integer in series.
else
{
s=1; //set ‘s’ variable as ‘1’ to generate next numbers of series.
while(t<n)
{
t=f+s; //Generating next number in the series by adding preceding two integers.
if(n==t)
flg=1;
f=s;
s=t; //Shifting the last two terms of series to ‘f’ & ‘s’ for generating next number in the series.
}
}
if(flg==1)
printf(“Given integer is a Fibonacci number.\n”);
else
printf(“Given integer is Not Fibonacci number.\n”);
return;
}
Output:
i) Enter a number : 610
Given integer is a Fibonacci number.
ii) Enter a number : 812
Given integer is Not Fibonacci number.
For improvement of your coding skills we give some tasks along with it. If you solve this tasks and send to our email (onlineexamshubteam@gmail.com) with your details (like Name, City, college, and photo). We will display your details in our site.
Task: Write a C program to print nth term in Fibonacci series.