Problem

( Compute factorials) Rewrite thefactorial function in Listing usingiterations.Listing Com...

( 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 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

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.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT