In mathematical terms, the sequence Fn of Fibonacci numbers is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
Write a function int fib(int n) that returns Fn. For example, if n = 0, then fib() should return 0,
PROGRAM: C
#include <stdio.h>
int fib(int ); //function declaration
int main() //main function
{
int n;
printf("Enter the number of terms:"); //reading number of
terms
scanf("%d",&n);
fib(n); //calling function fib
return 0;
}
int fib(int n) //function fib
{
if(n<=1)
{
printf("\nFibnocci series Fn:%d",n); //if n<=1 , print n(0 or
1)
}
else //else part
{
int a=0,b=1,c; //initializes a,b,c
printf("\nFibnocci series Fn:");
for(int i=0;i<n;i++)
{
printf("%d,",a); //print a
c=a+b; //setting c as the sum of first 2 numbers.
a=b; //setting the next two numbers
b=c;
}
}
}
OUTPUT
I Hope you got the idea.Thank You.
Get Answers For Free
Most questions answered within 1 hours.