The Code given below can be used in TURBO C++ Compilers: -
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
class Fibonacci
{
public:
int temp1, temp2, temp3;
Fibonacci()
{
temp1 = 0;
temp2 = 1;
temp3 = 0;
cout << temp1 << " " << temp2 << " ";
}
void fib(int n)
{
if (n > 0)
{
temp3 = temp1 + temp2;
temp1 = temp2;
temp2 = temp3;
cout << temp3 << " ";
fib(n - 1);
}
}
};
int main()
{
clrscr();
int n;
cout << "Enter n: ";
cin >> n;
Fibonacci F;
F.fib(n-2);
getch();
return 0;
}
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
class Fibonacci
{
public:
int temp1 = 0, temp2 = 1, temp3 = 0;
Fibonacci()
{
cout << temp1 << " " << temp2 << " ";
}
void fib(int n)
{
if (n > 0)
{
temp3 = temp1 + temp2;
temp1 = temp2;
temp2 = temp3;
cout << temp3 << " ";
fib(n - 1);
}
}
};
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
Fibonacci F;
F.fib(n-2);
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP