Use of recursion
using namespace std;
int factorial( int n)
{ if(n<2)
{ return 1; }
int prvsnmbr=factorial(n-1);
return n*factorial(n-1);
}
int sum(int n)
{ if (n<1)
{return 0;}
return (n+sum(n-1));
}
int fibonacciseries(int n)
{ if(n==1)
{ return 1;}
if(n==0)
{ return 0;}
int prevsnmbr = fibonacciseries(n-1)+fibonacciseries(n-2);
return prevsnmbr;}
int main()
{ int n;
cout <<"Enter the number = ";
cin>>n;
cout<<"factorial of nmbr = "<<factorial(n)<<endl;
cout <<"sum oh nth nmbr = "<<sum(n)<<endl;
cout<<"fibonacciseries of nmbr= "<<fibonacciseries(n)<<endl;
return 0;
}
Comments
Post a Comment
If you have any doubt ,let me know