Fibonacci Series in C++
Fibonacci Series in C++: In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.
Do the following two ways:
- Fibonacci Series without recursion
- Fibonacci Series using recursion
//Faizah Akter
ReplyDelete//202010061
//Fibonacci series without recursion
#include
using namespace std;
class B
{
public:
int check()
{
int num1=0,num2=1,num3,i,number;
cout<<"Please Enter the number of elements you want to print : ";
cin>>number;
cout<<num1<<" "<<num2<<" ";
for(i=2;i<number;++i)
{
num3=num1+num2;
cout<<num3<<" ";
num1=num2;
num2=num3;
}
return 0;
}
};
int main()
{
B obj;
obj.check();
}
//Faizah Akter
ReplyDelete//202010061
//Fibonacci series with recursion
#include
using namespace std;
void Fibonacci(int n)
{
static int num1=0, num2=1, num3;
if(n>0)
{
num3 = num1 + num2;
num1 = num2;
num2 = num3;
cout<>n;
cout<<"Series are : ";
cout<<"0 "<<"1 ";
Fibonacci(n-2);
return 0;
}
Mahbuburb Rahman Limon
ReplyDeleteId: 202030070
#include
using namespace std;
class Fibonacci{
public:
int a, b, c;
void generate(int);
};
void Fibonacci::generate(int n){
a = 0;
b = 1;
cout << a << " " <> n;
Fibonacci obj;
obj.generate(n);
return 0;
}
Mahbubur Rahman limon
ReplyDeleteId:202030070
<>
#include
using namespace std;
void fibonacci(int n)
{
static int num1=0,num2=1,num3=3;
if(n>0)
{
num3=num1+num2;
num1=num2;
num2=num3;
cout<>n;
cout<<"series are:";
cout<<"0"<<" 1 ";
fibonacci(n-2);
return 0;
}
#include
ReplyDeleteusing namespace std;
int main ()
{
int a[30],n,i;
cout<<"How many Fibonacci number:" ;
cin>>n;
a[0]=0;
a[1]=1;
for(i=2;i<n;i++)
{
a[i]=a[i-1]+a[i-2];
}
cout "\n";
for(i=0;i<n;i++)
{
cout"n" ,a[i] ;
}
}
ID:201520785
ReplyDeleteMahbubur Rahman Limon
ReplyDeleteId: 202030070
//with recursion using class and obj
#include
using namespace std;
class fib
{
public:
void fibonacci(int n)
{
static int num1=0,num2=1,num3=3;
if(n>0)
{
num3=num1+num2;
num1=num2;
num2=num3;
cout<>n;
cout<<"series are:";
cout<<"0"<<" 1 ";
obj.fibonacci(n-2);
return 0;
}
//Robin Ahmed
ReplyDelete//201910109
#include
using namespace std;
int main ()
{
int number,first=0,sec=1,next;
cout<<" Enter the number of terms to be printed in the fibonacci serice"<>number;
cout<<"fibonacci serice "<<endl;
for(int i=0; i<number;i++)
{
cout<<first<<endl;
next=first+sec;
first=sec;
sec=next;
}
}
//Monika Akter Ratna
ReplyDelete//202010275
#include
using namespace std;
int main (){
int num,fac = 1;
cout<< "enter the number "<>num;
if(num < 0)
cout << "can't find the factorial ";
else if (num<=1)
cout << num << "! = "<=2; con --)
{fac=fac*con;}
cout<< num << "! = "<<fac;
}
return 0;
}
//Monika Akter Ratna
ReplyDelete//202010275
#include
using namespace std;
int main ()
{
int number,a=0,b=1,c;
cout<<" Enter the number "<>number;
cout<<"Fibonacci serice "<<endl;
for(int i=0; i<number;i++)
{
cout<<a<<endl;
c=a+b;
a=b;
b=c;
}
}