Question

C++ //Write prototype for function factorial that accepts an int num and returns an int /*...

C++

//Write prototype for function factorial that accepts an int num and returns an int

/*

WITH LOOP OF YOUR CHOICE:

Write code for function factorial that accepts an int num

and returns the num's factorial

factorial(5);

1*2*3*4*5

returns 120

DON'T FORGET TO WRITE TEST CASE.

*/

//write includes statements

//write using statements for cin and cout


/*

Use a do while loop to prompt the user for

a number, call the factorial function, and display the number's

factorial. Also, loop continues as long as user wants to.

*/

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int factorial(int n);

int main() {
    int n;
    do {
        cout << "Enter a number(negative to stop): ";
        cin >> n;
        if (n >= 0) {
            cout << "Factorial of " << n << " is " << factorial(n) << endl;
        }
    } while (n >= 0);
    return 0;
}

int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}

Add a comment
Know the answer?
Add Answer to:
C++ //Write prototype for function factorial that accepts an int num and returns an int /*...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
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
ADVERTISEMENT
ADVERTISEMENT