Sunday, November 28, 2010

C Program For Fibonacci Series

Hello everybody I want to discuss about How to generate a Fibonacci series in C programming.

What is Fibonacci Series?


The first two Fibonacci numbers are 0 and 1 then next number is addition of previous two numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.....
In mathematics it is defined by recurrence relation.

Program Code


  


//C Program for generate Fibonacci series
#include "stdio.h"
#include "conio.h"
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}

Input\Output



61 comments:

susamoy said...

thanks.......

gaadi said...
This comment has been removed by the author.
raj said...

thanks

raviteja said...

can u write this program without taking 0,1 as initial values for a and b variables

shabha said...

wrong!!!!!

shabha said...

u hav printed 8 no's bt v ned only 6 no's???

Manigandan said...

if you take
a=-1
b=1
means u don't need to print a,b

swati said...

wow thanks its totally right :)

abhi said...

mind blowing its right

aravind said...

thanku

newB1e said...

dis is wrong dude ;
when u want 3rd number in the series (dat is 1) you hav to give value of n == 1...

Gagan said...

I hope this solves your problem
#include
#include
void main()
{
int a,b,c,i,n;
a=0;
b=1;
printf("Enter number of terms of the series to be generated:");
scanf("%d",&n);
if(n==1)
{
printf("\n FIBONACCI SERIES\n");
printf("%d",a);

}
else if(n==2)
{
printf("\n FIBONACCI SERIES\n");
printf("%d\t%d",a,b);

}
else
{
printf("%d\t%d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);

}
}
getch();
}

akshay76 said...

void main()
{
int a,b,c,i,n;
a=0;
b=1;
printf("Enter number of terms of the series to be generated:");
scanf("%d",&n);
if(n==1)
{
printf("\n FIBONACCI SERIES\n");
printf("%d",a);

}
else if(n==2)
{
printf("\n FIBONACCI SERIES\n");
printf("%d\t%d",a,b);

}
else
{
printf("%d\t%d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);

}
}
getch();
}

OR

void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}

naveen said...

well, the for loop should be
for(i=1;i<=n-2;i++)

Subin Priyan said...

good one re, thank you

priyan said...

thanks..

M Ay said...

Thanks!!!!!!!!!!!
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}

mahabub & sayan said...

this is the best.........

#include
#include
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=1;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}

ZuBaiR said...

hey can u post flow chart for this !!! plzz

Lavish said...

fibonacci series starts with 1 never with 0...

SANTHOSH. said...
This comment has been removed by the author.
DHIVYA. said...

Thank you.

66 said...

a= -1

b=1

sum = a+b

a=b

b=sum

madhubalini said...

fibonacci series
#include
#include
void main()
{
int i,n,c,a=0,b=1;
clrscr();
printf("enter the number");
scanf("\n%d\n",&n);
printf("\nFIBONACCI SERIES\n");
printf(%d\n%d\n",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d\n",c);
}
getch();
}

sonam said...

thank u..

karam said...

can explain the running of loop?

Varun said...

//This program will print the fibonacci series by using the printf statement only once...have a close look!!!

fibonacci series
#include
#include
void main()
{
int i,n,c,a=0,b=1;
clrscr();
printf("enter the number");
scanf("\n%d\n",&n);
printf("\nFIBONACCI SERIES\n");

for(i=0;i<=n;i++)
{
printf("%d\n",a);
c=a+b;
a=b;
b=c;

}
getch();
}

mudit said...

can anyone tell how to write reverse fabonnaci series

M Junaid Awan. said...

#include
#include
void main()
{
int i,n,c,a=0,b=1;
clrscr();
printf("enter the number");
scanf("\n%d\n",&n);
printf("\nFIBONACCI SERIES\n");

for(i=n;i<n;i--)
{
printf("%d\n",a);
c=a+b;
a=b;
b=c;

}
getch();
}

Arnab said...

idiot

making it complex

snigdho1993 said...

It's running successfully.......... thanks..

Laxman Koirala said...
This comment has been removed by the author.
Laxman Koirala said...

#include

main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");
scanf("%d",&n);

printf("First %d terms of Fibonacci series are :-\n",n);

for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}

return 0;
}

Mayur Ujagare said...

Hey Frnz I Have Simple Way To Print it And Hope u Like N try it
so let's begin

void main(){

int a,b,i,n;

clrscr();

n=10;

a=0;

b=1;

for(i=1; i<=n; i++){

a=a+b;

b=a-b;

printf("%d ",b);


}

getch();}

the value of 'n' Will Varies as the series wants to maximize

abhay said...

hi, u have oversimplified...

the series is 0,1,1,2,3,5,8... while ur program gives
0,1,2,3,5,8....
so the third term gets missing...
but good attempt....

i think it is best to use
for(couter=1;counter<=N-2;counter++) loop....
:-)

nandini visu said...

the program is wrong.. publish the correct coding;;;;

anasmoosa mathrakkattu said...

u have to give for loop as for(i=2;i<n;i++)
remaining thnks are ok................

Sai Kiran said...

/* Fibonacci Series c language */
#include

main()
{
int n,a=0,b=1,c,d;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for(d=0;d<n;d++)
{
if (d<=1)
c=d;
else
{
c=a+b;
a=b;
b=c;
}
printf("%d\n",c);
}
getch();
}

Sai Kiran said...

Other way without using "if else"

/* Fibonacci Series c language */
#include

main()
{
int n,a=0,b=1,c,d;

printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :- \n",n);
printf("%d\n%d\n",a,b);

for(d=0;d<n-2;d++)
{
c=a+b;
a=b;
b=c;

printf("%d\n",c);
}
getch();
}

Sai Kiran said...

/* Fibonacci Series c language */
#include

main()
{
int n,a=0,b=1,c,d;

printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :- \n",n);
printf("%d\n%d\n",a,b);

for(d=0;d<n-2;d++)
{
c=a+b;
a=b;
b=c;

printf("%d\n",c);
}
getch();
}

Darshan Hathinarayan said...

if i enter the no 1 than it will print 0 and 1 both in place of zero
i think it's not proper output

slyVESter said...

All above programs are starting with 0 and 1, what if we want another values say -1 or other integer ? Try this one !
#include
#include
int main(void)
{
int a,b,c,i,n;
printf("Enter first number: ");
scanf("%d", &a);
printf("\nEnter the second number:");
scanf("%d", &b);
printf("\nHow many times does you want the series:");
scanf("%d",&n);
if(n==1)
{
printf("\nFibonacci series is:\t%d", a);
}
else if(n==2)
{
printf("\nFibonacci series is:\t%d\t%d", a,b);
}
else
{
printf("\nFibonacci series is:\t%d\t%d", a,b);
for(i=2; i<n; i++)
{
c= a+b;
a= b;
b= c;
printf("\t%d", c);
}
}
getch();
}

S Chandrababu said...
This comment has been removed by the author.
S Chandrababu said...

#include
main()
{
int i,n,a,b;
printf("enter n terms");
scanf("%d",&n);
printf("fibonacci siries\n");
for(i=0;i<n;i++)
{
a=a+b;
b=a+b;
printf("%d %d",a,b);
}
}

ash said...

Its simplest n perfect code...!!!

TakNeh said...

int main()
{
int a, b, fib, n=10,i;

a=0;b=1;

for (i=0;i<n;i++)
{
printf("fib %d\n", a);
fib = a+b;
a=b;
b=fib;
}
return 0;
}

monika pattanayak said...

#include
#include
void main()
{
int a,b,c,i,n;
a=0;
b=1;
printf("Enter number :");
scanf("%d",&n);
if(n==1)
{
printf("\n fibonacci series\n");
printf("%d",a);

}
else if(n==2)
{
printf("\n fibonacci series\n");
printf("%d\t%d",a,b);

}
else
{
printf("%d\t%d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);

}
}
getch();
}

SUNDAR said...

for(i=0;i<n-2;i++)

SUNDAR said...

Jst Change the for loop



for(i=0;i<n-2;i++)

harish said...

#include
void main()
{ int n,i,a,b,c;
printf("enter the range");
scanf ("%d",&n);
for(i=o;i<=n;i++)
{c=a+b;
a=b;
b=c;
printf("the fibonaci series is=%d",c)
}
getch();
}

Shivani Kamboj said...

logic is correct and simple but it's not working.

Prasun said...

void main()
{
int a=-1,b=1,c,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d\t",c);
}
getch()
}

amit agarwal said...

#include
#include
main()
{int a,b,i,n;
a=b=1;
printf("enter number of times u want to print fibb series");
scanf("%d",&n);
for(i=0;i<n;i++)
{if(i<=1)
printf("%d",1);
c=a+b;
a=b;
b=c;
printf("%d",c);
}
getch();
}



it will print

1 1 2 3 5 7 12 19.....

Santhosh K N said...

No we can't write this code without intiala'n.

vishwambhar pandey said...

void main()
{
int i,n;
clrscr();
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
for(i=0;i<n;i++)
{
cout<<fabo(i)<<" ";
}
getch();
}

fabo(n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return fabo(n-1)+fabo(n-2);
}
}

Akash said...

I dont know if u right...but what we were taught was starting from zero itself...well maybe u r right....lets see....

lil wayne said...

#include
void main(){
int no,x=1,y=1,z,i;
printf("Enter number :");
scanf("%d",&no);
for(i=2;i<no;i++)
{
z=x+y;
x=y;
y=x;
}
printf("%d",Z);
getch();
}

}

vishal kumar said...
This comment has been removed by the author.
vishal kumar said...

chek out this as well : c program to find fibonacci series using recursion

Hadando Guy said...


2. Draw the flowchart and write c program to display Fibonacci series numbers

CHETAN REDDY said...

can u wtite a code to print the fibonacci series within a specified range by the user. like if user enters 10 as the range,program should print 0,1,1,2,3,5.8

Post a Comment