Using the recursive algorithm described in class, illustrate by breaking the pro
ID: 2247375 • Letter: U
Question
Using the recursive algorithm described in class, illustrate by breaking the problem, how would you compute the 8th number in the Fibonacci series, i.e. show how you would compute F(8). What would be the runtime when you complete your computation?
Write a C++ Program to incorporate both the iterative and recursive function to compute factorial of an integer N.
The pseudocode for factorial computations are given as below:
(In pseudocode convention, <- is assignment operator, = is comparison, for loop start and end indentations are used.
Iterative
Function Factorial_Iterative (N)
Begin
Fact <- 1
I <- 1
While I <= N
Fact <- Fact * I
I <- I + 1
End of while loop
Return Fact
End of Function
Recursive
Function Factorial_Recursive(int N)
If N = 0 or N = 1 return N
Else
Return N * Factorial_Recursive(N-1)
End of Function
Your program must use a main and get an user input for N. Then call each of the function, one at a time to compute and display the value of Factorial. Run your code with N = 5, 10 and 15. What do you observe. Do you agree with the results obtained. Comment why or why not.
Explanation / Answer
Answer:-
Program for Fabonacii series is:-
#include<iostream>
using namespace std;
int fab(int n,int a,int b){
if(n==0)
return 0;
else
{
int c=a+b;
cout<<" "<<c;
return fab(n-1,b,c);
}
}
int main(){
int n;
cout<<"Enter no";
cin>>n;
if(n==1)
cout<<"0";
else if(n==2)
cout<<"0 1";
else{
cout<<"0 1";
fab(n,0,1);
}
return 0;
}
Output:-
Enter no8
0 1 1 2 3 5 8 13 21 34
#include<iostream>
using namespace std;
int fact(int n){
if(n==0)
return 1;
else
{
return (n*fact(n-1));
}
}
int fact1(int n){
int i,f;
for(i=1;i<=n;i++){
f*=i;
}
return f;
}
int main(){
int n;
cout<<"Enter no";
cin>>n;
int ans=fact(n);
int ans1=fact1(n);
cout<<ans<<ans1;
return 0;
}
for n=5 in both case answer is 120
for n=10 in both case answer is 3628800
for n=15 in both case answer is 2004310016
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.