Question: Write a C++ program that calls a function factorial to calculate n! where n is a positive integer. Use a for loop in the main

Write a C++ program that calls a function factorial to calculate n! where n is a positive integer. Use a for loop in the main program to test more than one case. Include a while loop for the error check, n must be positive. Use a for loop within the function to calculate the factorial.

*See guide below*

#include using namespace std;

// Function Prototype int factorial (int num); int main() { DECLARATION OF VARIABLES cout << "How many test cases: "; INPUT cases cout << endl; FOR LOOP loop for each test case USE i AS THE LOOP COUNTER { cout << " Test Case #" << i << ": " << endl; INPUT num WITH PROMPTING MESSAGE // Error Check - while LOOP WHILE LOOP while num IS LESS THAN ZERO { cout << "Invalid Entry - n must be positive. "; INPUT num WITH PROMPTING MESSAGE } // Function Call result = factorial(num); OUTPUT THE result } cout << endl; return 0; } // Function Definition int factorial(int num) { DECLARATION OF LOCAL VARIABLES INITIALIZE fact TO ONE FOR LOOP loop num times USE count AS THE LOOP COUNTER MULTIPLY fact BY count STORING RESULT IN fact RETURN STATEMENT }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!