Answer:
Here is the C++ program:
#include <iostream> //to use input output functions
using namespace std; //to access objects like cin cout
void PrintFactorial(int factCounter, int factValue){ // method definition
int nextCounter = 0; // initialize value of nextCounter by 0
int nextValue = 0; // initialize value of nextValue by 0
if (factCounter == 0) { //base case 1: 0! = 1
cout << "1" << endl; } //displays 1 in the output
else if (factCounter == 1) { //base case 2: 1! = 1
cout << factCounter << " = " << factValue << endl; } //displays 1 in result
else { //recursive case
cout << factCounter << " * ";
nextCounter = factCounter - 1; //decrements factCounter by 1
nextValue = nextCounter * factValue; // multiplies values of nextCounter and factValue and assigns the result to nextValue
PrintFactorial( nextCounter, nextValue); } } //recursively calls PrintFactorial method to compute the factorial
int main() { //start of main function
int userVal = 0; //initialize the userVal variable to 0
userVal = 5; //assigns 5 to the userVal variable
cout << userVal << "! = "; //prints the userVal with ! =
PrintFactorial(userVal, userVal); } //calls PrintFactorial method to compute the factorial of userVal i.e. 5
Explanation:
I will explain the logic of the program with an example.
The value of variable userVal is 5
This is passed to the method PrintFactorial()
1st step:
void PrintFactorial(int factCounter, int factValue) becomes:
PrintFactorial(5, 5)
PrintFactorial method has two base conditions which do not hold because the factCounter is 5 here which is neither 1 nor 0 so the program control moves to the recursive part.
cout << factCounter << " * ";
In recursive part, the above statement prints the value of factCounter followed by an asterisk on the output screen. So this prints:
5 *
nextCounter = factCounter - 1;
This statement decrements the value of factCounter by 1 and assigns it to nextCounter as:
nextCounter = 5 - 1;
nextCounter = 4
nextValue = nextCounter * factValue; multiplies the value of nextCounter by factValue and stores result in nextValue . So
nextValue = 4 * 5
nextValue = 20
PrintFactorial( nextCounter, nextValue); this statement calls the PrintFactorial() recursively to perform the above steps again until the base condition is reached. This statement becomes:
PrintFactorial(4,20)
2nd step:
PrintFactorial method has two base conditions which do not hold because the factCounter is 4 here which is neither 1 nor 0 so the program control moves to the recursive part.
cout << factCounter << " * ";
In recursive part, the above statement prints the value of factCounter followed by an asterisk on the output screen. So this prints:
5* 4 *
nextCounter = factCounter - 1;
This statement decrements the value of factCounter by 1 and assigns it to nextCounter as:
nextCounter = 4 - 1;
nextCounter = 3
nextValue = nextCounter * factValue; multiplies the value of nextCounter by factValue and stores result in nextValue . So
nextValue = 3 * 20
nextValue = 60
PrintFactorial( nextCounter, nextValue); This statement becomes:
PrintFactorial(3,60)
3rd step:
PrintFactorial method has two base conditions which do not hold because the factCounter is 3 here which is neither 1 nor 0 so the program control moves to the recursive part.
cout << factCounter << " * ";
In recursive part, the above statement prints the value of factCounter followed by an asterisk on the output screen. So this prints:
5* 4 * 3 *
nextCounter = factCounter - 1;
This statement decrements the value of factCounter by 1 and assigns it to nextCounter as:
nextCounter = 3 - 1;
nextCounter = 2
nextValue = nextCounter * factValue; multiplies the value of nextCounter by factValue and stores result in nextValue . So
nextValue = 2 * 60
nextValue = 120
PrintFactorial( nextCounter, nextValue); This statement becomes:
PrintFactorial(2,120)
4th step:
PrintFactorial method has two base conditions which do not hold because the factCounter is 2 here which is neither 1 nor 0 so the program control moves to the recursive part.
cout << factCounter << " * ";
In recursive part, the above statement prints the value of factCounter followed by an asterisk on the output screen. So this prints:
5* 4 * 3 * 2 *
nextCounter = factCounter - 1;
This statement decrements the value of factCounter by 1 and assigns it to nextCounter as:
nextCounter = 2 - 1;
nextCounter = 1
nextValue = nextCounter * factValue; multiplies the value of nextCounter by factValue and stores result in nextValue . So
nextValue = 1 * 120
nextValue = 120
PrintFactorial( nextCounter, nextValue); This statement becomes:
PrintFactorial(1,120)
Now the base case 2 evaluates to true because nextCounter is 1
cout << factCounter << " = " << factValue statement prints the value of factCounter i.e. 1 followed by an equal sign = followed by value of factValue i..e 120
So the output of the above program with userVal = 5 is:
5! = 5 * 4 * 3 * 2 * 1 = 120