/* Program to find the factorial, GCD and Fibonacci using recursion */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int fact(int);
int gcd(int,int);
void fib(int,int,int,int);
void main()
{
char ch;
clrscr();
while(1)
{
cout<<"\n\n************RECURSIVE SOLUTIONS*************\n\n";
cout<<"1.Factorial of a number \n2.Fibonacci Series\n3.GCD of 3 numbers\n4.Exit";
cout<<"\nEnter your choice:";
ch=getche();
switch(ch)
{
case '1': cout<<"\nEnter a number:";
int n,f;
cin>>n;
f=fact(n);
cout<<"\n"<<n<<"!="<<f;
break;
case '2': cout<<"\nEnter the limit:";
cin>>n;
cout<<"\nFibonacci Series \n";
if(n==0)
cout<<"0";
else if(n==1)
cout<<"0,1";
else
{
cout<<"0,1,";
fib(0,1,1,n);
}
break;
case '3': cout<<"\nEnter 3 numbers\n";
int n1,n2,n3;
cin>>n1>>n2>>n3;
cout<<"GCD of 3 numbers is:"<<gcd(n1,gcd(n2,n3));
break;
case '4': exit(0);
default : cout<<"Invalid choice!";
}
}
}
int fact(int x)
{
int f;
if(x==0)
return 1;
else
f=x*fact(x-1);
return f;
}
void fib(int n1,int n2,int n3,int l)
{
n3=n1+n2;
if(n3>l)
return;
cout<<n3<<",";
fib(n2,n3,n3,l);
}
int gcd(int n1,int n2)
{
if(n2==0)
return n1;
else if(n2>n1)
return gcd(n2,n1);
else
return gcd(n2,n1%n2);
}
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int fact(int);
int gcd(int,int);
void fib(int,int,int,int);
void main()
{
char ch;
clrscr();
while(1)
{
cout<<"\n\n************RECURSIVE SOLUTIONS*************\n\n";
cout<<"1.Factorial of a number \n2.Fibonacci Series\n3.GCD of 3 numbers\n4.Exit";
cout<<"\nEnter your choice:";
ch=getche();
switch(ch)
{
case '1': cout<<"\nEnter a number:";
int n,f;
cin>>n;
f=fact(n);
cout<<"\n"<<n<<"!="<<f;
break;
case '2': cout<<"\nEnter the limit:";
cin>>n;
cout<<"\nFibonacci Series \n";
if(n==0)
cout<<"0";
else if(n==1)
cout<<"0,1";
else
{
cout<<"0,1,";
fib(0,1,1,n);
}
break;
case '3': cout<<"\nEnter 3 numbers\n";
int n1,n2,n3;
cin>>n1>>n2>>n3;
cout<<"GCD of 3 numbers is:"<<gcd(n1,gcd(n2,n3));
break;
case '4': exit(0);
default : cout<<"Invalid choice!";
}
}
}
int fact(int x)
{
int f;
if(x==0)
return 1;
else
f=x*fact(x-1);
return f;
}
void fib(int n1,int n2,int n3,int l)
{
n3=n1+n2;
if(n3>l)
return;
cout<<n3<<",";
fib(n2,n3,n3,l);
}
int gcd(int n1,int n2)
{
if(n2==0)
return n1;
else if(n2>n1)
return gcd(n2,n1);
else
return gcd(n2,n1%n2);
}
No comments:
Post a Comment