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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started