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.
Guide:
// Purpose of Program
// Name:
// Filename: LASTNAMElab7b.cpp
#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
Get step-by-step solutions from verified subject matter experts
