Q1.make a class of fabnacci series. This series is 1 1 2 3 5 8 13 21 and so on.
#include <iostream>
using namespace std;
class Fibonacci{
int* n, x;
public:
Fibonacci(int y): x(y){
n = new int[x];
for(int i = 0; i < x; i++){
if(i == 0) n[i] = 0;
else if(i == 1) n[i] = 1;
else n[i] = n[i - 1] + n[i - 2];
}
}
void show(){
for(int i = 0; i < x; i++) cout<<n[i]<<" ";
}
};
int main(){
cout<<"Input number of fibonacci numbers to generate: ";
int n; cin>>n;
Fibonacci fib(n);
fib.show();
return 0;
}
Comments
Leave a comment