Answer to Question #326144 in C++ for Wasim Akram

Question #326144

Program to print fibonacci series using classes and objects

1
Expert's answer
2022-04-09T17:54:25-0400
#include <iostream>
// C++ Program to print Fibonacci

using namespace std;
 
// Creating class for Fibonacci.
class Fibonacci {
 
public:
    int a, b, c;
    void generate(int);
};
 
void Fibonacci::generate(int n) {
    a = 1;
    b = 1;
 
    cout << a << " " << b;
      for (int i = 1; i <= n - 2; i++) {
          c = a + b;
          cout << " " << c;
 
        a = b;
        b = c;
    }
}

int main()
{
    int n;
 
    Fibonacci fib;
    cout<<"Enter N ";
    cin >> n;
    fib.generate(n);
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS