Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write code to complete printFactorial()\'s recursive case. Sample output if user

ID: 3829169 • Letter: W

Question

Write code to complete printFactorial()'s recursive case. Sample output if userVal is 5: 5! = 5 * 4 * 3 * 2 * 1 = 120

public class RecursivelyPrintFactorial {
public static void printFactorial(int factCounter, int factValue) {
int nextCounter = 0;
int nextValue = 0;

if (factCounter == 0) { // Base case: 0! = 1
System.out.println("1");
}
else if (factCounter == 1) { // Base case: Print 1 and result
System.out.println(factCounter + " = " + factValue);
}
else { // Recursive case
System.out.print(factCounter + " * ");
nextCounter = factCounter - 1;
nextValue = nextCounter * factValue;

/* Your solution goes here */

}
}

public static void main (String [] args) {
int userVal = 0;

userVal = 5;
System.out.print(userVal + "! = ");
printFactorial(userVal, userVal);

return;
}
}

Explanation / Answer

public class RecursivelyPrintFactorial {
   public static void printFactorial(int factCounter, int factValue) {
   int nextCounter = 0;
   int nextValue = 0;
   if (factCounter == 0) { // Base case: 0! = 1
   System.out.println("1");
   }
   else if (factCounter == 1) { // Base case: Print 1 and result
   System.out.println(factCounter + " = " + factValue);
   }
   else { // Recursive case
   System.out.print(factCounter + " * ");
   nextCounter = factCounter - 1;
   nextValue = nextCounter * factValue;
   /* Your solution goes here */
   printFactorial(nextCounter,nextValue);
   }
   }
   public static void main (String [] args) {
   int userVal = 0;
   userVal = 5;
   System.out.print(userVal + "! = ");
   printFactorial(userVal, userVal);
   return;
   }
   }

______________________

output:

5! = 5 * 4 * 3 * 2 * 1 = 120

__________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote