Fibonacci series:The first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the previous two

e.g.: 0 1 1 2 3 5 8 13 21 ....

A number n is taken from user and the series is displayed upto nth term.

Select To use this code as it is.. select and copy paste this code into code.cpp file :)

- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int a=0,b=1,c=0,n;
- cout<<"Enter the number of terms you wanna see: ";
- cin>>n;
- cout<<a<<" "<<b<<" ";
- for(int i=1;i<=n-2;i++)
- {
- c=a+b;
- a=b;
- b=c;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- cout<<c<<" ";
- }
- getch();
- }

## 14 comments:

in FOR statement "i<=n" will come :)

if i<=n is taken then the no of terms displayed is 17...!

#include

using namespace std;

class fib

{

int i,n,f1,f2,f3;

public:

void get();

void put();

};

void fib::get()

{

cout<<"enter the no.";

cin>>n;

}

void fib::put()

{

f1=0,f2=1;

cout<<f1<<endl<<f2<<endl;

i=0;

while(i<n)

{

f3=f1+f2;

cout<<endl<<f3<<endl;

f1=f2;

f2=f3;

++i;

}

}

int main()

{

fib a1;

a1.get();

a1.put();

}

~

#define fibonacci

#include

void main()

{

int a,b,c;

for(a=0,b=1,c=0;c<10;c=a+b,a=b,b=c)

cout<<c;

}

AM I CORRECT? BUT ITS NOT FOR N TERMS.. O/P S BASED ON VALUE OF C... PLS REPLY FOR ANY CORRECTION

can u specify #fibonacci jus like dat?? sorry if this is silly

instead of for(int i=1;i<=n-2;i++)

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

its show perfect series

it shows correct answers.. thanks

the programs on this site are so useful for computer science students..........

C program to convert given number of days into years,weeks and days#include

#include

#include

int main()

{

int a,b,c,n;

a=0; b=1;

clrscr();

cout<<”enter number of terms (n)=”;

cin>>n;

cout<<Fibonacci series is”<<end1;

cout<<a<<setw(4)<<b;

int count=3;

while (count<=n)

{

c=a+b;

cout<<setw(4)<<c;

a=b;

b=c;

count++;

}

getch();

return 0;

}

can u plz post a program of fibonacci series using a another function

void fib(int n)

#include

#include

void main()

{

clrscr();

{

int n,a=0,b=1,c,i;

cout<<"enter the value of n";

cin>>n;

cout<<"fibonacci series is=";

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

{

if(i<=1)

{

c=i;

}

else

c=a+b;

a=b;

b=c;

}

cout<<c<<",";

}

getch();

}

u r already printing the first two numbers

See this one.. little more advanced..

actually whoever the programmer is doesn't know anything related to programming... i am atleast a 100 times better programmer....

Post a Comment