Program to print fibonacci series using classes and objects
#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;
}
Comments
Leave a comment