0% found this document useful (0 votes)
21 views

Week 4 Assignment

Uploaded by

iirwed79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Week 4 Assignment

Uploaded by

iirwed79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1-Write a program that returns the factorial of a number by using recursive

function?

#include <iostream>
using namespace std;
int Fact(int );
int main()
{
int num;
cout<<"Enter a number : "<<endl;
cin>>num;
cout<<Fact(num);
}
int Fact(int N)
{
if(N==0 or N==1)
return 1;
else
return N*Fact(N-1);
}
2- Write a program that returns the power of a number by using recursive
function?

#include <iostream>
using namespace std;
int Power(int, int);
int main()
{
int b,n;
cout<<"Enter the base : "<<endl;
cin>>b;
cout<<"Enter the exponent : "<<endl;
cin>>n;
cout<<"The result is : "<<Power(b,n);
}
int Power(int X, int N)
{
if(N==1)
return X;
else
return X*Power(X,N-1);
}
3-Write a program that applies fibonacci by using recursive function?

#include <iostream>
using namespace std;
void Fib(int, int, int);
int main()
{
int a=0,b=1,n;
cout<<"Enter number of sequence : "<<endl;
cin>>n;
Fib(a,b,n);
}
void Fib(int A, int B, int N)
{
if(N>0)
{
int C=A+B;
cout<<C<<endl;
Fib(B,C,N-1);
}
}

4-Write a program that returns the least common multiple by using recursive
fumction?

#include <iostream>
using namespace std;
int LCM(int Sum, int A, int B);
int main()
{
cout<<LCM(8,8,12);
}
int LCM(int Sum, int A, int B)
{
if(Sum%B==0)
return Sum;
else
return LCM(Sum+A,A,B);
}

You might also like