8. Write a Program to generate Fibonacci Series by using Constructor to initialize the Data Members.

// FIBONACCI SERIES using CONSTRUCTOR



#include<iostream>
 #include<iomanip>
 
class fibonacci 
{
            int n1,n2;
public:
            fibonacci()
            {
                        n1 = 0;  n2 = 1;
            }
            void series(int);
};

void fibonacci::series(int n)
{ 
            int i,next;
            cout << setw(4) << n1 << setw(4) << n2;
            for(i=1; i <= n-2; i++)
            {
                        next = n1 + n2;
                        cout << setw(4) << next;
                        n1 = n2;  n2 = next;
            }
}

int main() 
{
            fibonacci fib;
            int n;
            cout << "FIBONACCI SERIES\n";
            cout << "How many numbers? ";
            cin >> n;
            fib.series(n);
}
Posted by Unknown On 01:58 5 comments

5 comments:

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *