1. Write a program factorial.cpp that uses recursion to multiply numbers 1 throu
ID: 3622612 • Letter: 1
Question
1. Write a program factorial.cpp that uses recursion to multiply numbers 1 through n. Print out the answer.
Sample execution:
Enter a number? 4 The factorial of 4 is 24
#include<iostream>
using namespace std;
int fact(int n){
int f = 1;
for ( int i=0; i <= n;i++)
f=i*f;
return f;
}
int main() {
cout<<"Enter a number?";
cin<<mul;
cout<<"The factorial of"<<n" is " <<m<<endl;
}
Thats the codewrote for this problem but i keep getting a error I'm wondering what I did wrong and how can I fix it.... =]
Explanation / Answer
The program you have written does not use recursion and also has mistakes. I have corrected the mistakes //factorial using iteration #include using namespace std; int fact(int n){ int f = 1; for ( int i=1; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.