Saturday, 11 March 2017

C Program to Print Fibonacci Series



In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:

1,1,2,3,5,8,13,21,34,55,89,144...
Often, especially in modern usage, the sequence is extended by one more initial term:

0,1,1,2,3,5,8,13,21,34,55,89,144...



By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence F(n) of Fibonacci numbers is defined by the recurrence relation:

F(n) = F(n-1) + F(n-2)

It defines that the the nth term of the sequence is sum of two previous terms.

F(4) = F(3) + F(2) = 1 + 1 = 2 

Here is a non-recursive  code of C Program to print first 'n' terms of Fibonacci Series

#include<stdio.h>
void main()
{
int a=-1,b=1,n,i,c;
printf("\n************************Fibonacci Series****************************\n");
printf("Enter number of terms you want to print\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d\t",c);
}
}

Here is a recursive  version code of C Program to print first 'n' terms of Fibonacci Series

#include<stdio.h>
int fib(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else
return fib(n-1)+fib(n-2);

}
void main()
{
int n,s,i=1;
clrscr();
printf("\n************************Fibonacci Series****************************\n");
printf("Enter number of terms you want to print\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=fib(i);
printf("%d\t",s);
}
}
Output::



C Program to print first 'n' terms of Fibonacci Series

non-recursive code of C Program to print first 'n' terms of Fibonacci Series
Fibonacci series in c
recursive version code of C Program to print first 'n' terms of Fibonacci Series
c program for Fibonacci series without and with recursion.
best program to print fibonacci series


Fibonacci Series algorithm ,Fibonacci Series non-recursive algorithm ,Fibonacci Series iterative algrothim ,Fibonacci Series recursive program in c,

No comments:

Post a Comment