C++ program to generate Fibonacci series

Today we are going to write a program for Fibonacci series in c++ language which is very simple.We are going to use classes and functions in our program.BY definition Fibonacci series start’s by 0 and 1,from then,if we need a number in the series then we need to add two numbers which are previous to our required number position.i.e If we need to get the third number then we must add first and second numbers.In the series first and second numbers are 0 and 1.Hence the third number will be 0+1=1.By this way we can find the numbers in the series.Let us write the code for the program…

#include<iostream.h>
#include<conio.h>
class fib
{
  public:
  int a,b,c;
  void generator(int);
};
void fib::generator(int n)
{
      a=0,b=1;
      cout<<a<<" "<<b;
      for(int i=1;i<=n-2;i++)
       {
	 c=a+b;
	 cout<<" "<<c;
	 a=b;
	 b=c;
	}
   }
void main()
{
  clrscr();
  int n;
  cout<<"Enter number of terms you need in the series:";
  cin>>n;
  fib ob;
  ob.generator(n);
  getch();
}

Output:
fib_001
You can download the source code:download

Advertisements

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my Master's in Computer Science.
This entry was posted in C++ and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

One Response to C++ program to generate Fibonacci series

  1. techtopz says:

    hey great post…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s