W.A.P. C++

WAP | CPP | Programs in C++ | C++ Solutions

write a program to display fibonacci series upto n terms


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 :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a=0,b=1,c=0,n;
  7. cout<<"Enter the number of terms you wanna see: ";
  8. cin>>n;
  9. cout<<a<<" "<<b<<" ";
  10. for(int i=1;i<=n-2;i++)
  11. {
  12. c=a+b;
  13. a=b;
  14. b=c;
  15. //Coding by: Snehil Khanor
  16. //http://WapCPP.blogspot.com
  17. cout<<c<<" ";
  18. }
  19. getch();
  20. }

14 comments:

Saalim's innovations April 24, 2011 at 8:34 PM  

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

Deepak_Pappu June 10, 2011 at 7:59 AM  

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

ap August 2, 2011 at 12:58 PM  

#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();
}
~

Kaushik Raja October 8, 2011 at 12:09 PM  

#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

Anonymous November 20, 2011 at 6:04 PM  

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

Vishwa January 12, 2012 at 9:00 AM  

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

its show perfect series

Anonymous February 15, 2012 at 8:04 PM  

it shows correct answers.. thanks

The C Programs May 23, 2012 at 5:59 PM  

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

preeya September 6, 2012 at 7:59 PM  

#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;
}

Anonymous November 28, 2012 at 8:42 PM  

can u plz post a program of fibonacci series using a another function
void fib(int n)

sameer gupta April 16, 2013 at 3:11 AM  

#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();
}

Anonymous September 2, 2013 at 7:05 PM  

u r already printing the first two numbers

Jasir September 18, 2013 at 11:31 PM  

See this one.. little more advanced..

Anonymous October 2, 2013 at 6:01 PM  


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

Search

About WAP C++

Here you'll find a wide range of programs' solution ranging from beginer level to advanced level.
All programs here are made, compiled and tested by me..and are running absolutely fine.. still if you find any bug in any program do let me know :)

Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner

my Binary Log

Recent Posts

Blog Archive



eXTReMe Tracker