( Compute factorials) Rewrite thefactorial function in Listing using
iterations.
Listing ComputeFactorial.cpp
1 #include
2 using namespace std;
3
4 // Return the factorial for a specified index
5 int factorial(int);
6
7 int main()
8 {
9 // Prompt the user to enter an integer
10 cout << "Please enter a non-negative integer: ” ;
11 int n;
12 cin >> n;
13
14 // Display factorial
15 cout <<"Factorial of " << n <<" is " << factorial(n);
16
17 return 0 ;
18 }
19
20 // Return the factorial for a specified index
21 int factorial(int n)
22 {
23 if (n == 0 ) // Base case
24 return 1 ;
25 else
26 return n * factorial(n - 1 ) ; // Recursive call
27 }
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.