C PROGRAM ONLY! Write code to complete PrintFactorial()\'s recursive case. Sampl
ID: 3601561 • Letter: C
Question
C PROGRAM ONLY! Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5:
#include
void PrintFactorial(int factCounter, int factValue){
int nextCounter = 0;
int nextValue = 0;
if (factCounter == 0) { // Base case: 0! = 1
printf("1 ");
}
else if (factCounter == 1) { // Base case: Print 1 and result
printf("%d = %d ", factCounter, factValue);
}
else { // Recursive case
printf("%d * ", factCounter);
nextCounter = factCounter - 1;
nextValue = nextCounter * factValue;
/* Your solution goes here */
}
}
int main(void) {
int userVal = 0;
userVal = 5;
printf("%d! = ", userVal);
PrintFactorial(userVal, userVal);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
void PrintFactorial(int factCounter, int factValue){
int nextCounter = 0;
int nextValue = 0;
if (factCounter == 0) { // Base case: 0! = 1
printf("1 ");
}
else if (factCounter == 1) { // Base case: Print 1 and result
printf("%d = %d ", factCounter, factValue);
}
else { // Recursive case
printf("%d * ", factCounter);
nextCounter = factCounter - 1;
nextValue = nextCounter * factValue;
/* Your solution goes here */
return PrintFactorial(factCounter-1, nextValue);
}
}
int main(void) {
int userVal = 0;
userVal = 5;
printf("%d! = ", userVal);
PrintFactorial(userVal, userVal);
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.