Write an iterative version (using a loop instead of recursion) of the factorial
ID: 3639321 • Letter: W
Question
Write an iterative version (using a loop instead of recursion) of the factorial function show in this chapter. Demonstrate the use of the function in a program that prints the factorial of a number entered by the userHere is what I have so far...
#include <iostream>
using namespace std;
// Function prototype
int factorial(int);
int main()
{
int number;
// Perform some simple tests first
for (int i = 1; i < 6; i++) {
cout << i << "! = " << factorial(i) << endl;
}
cout << endl;
// Then prompt the user for another value
cout << "Enter an integer value and I will display its factorial: ";
cin >> number;
cout << "The factorial of " << number << " is ";
cout << factorial(number) << endl;
}
Explanation / Answer
/* Iterative Version */ unsigned int iter_factorial(unsigned int n) { unsigned int f = 1; for(unsigned int i = 1; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.