Write code to complete PrintFactorial()’s recursive case. Sample output if userVal is 5:

Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5:

5! = 5 4 3 2 1 = 120

Answer

  1. #include <stdio.h>
  2. void PrintFactorial(int factCounter, int factValue){
  3. int nextCounter = 0;
  4. int nextValue = 0;
  5. if (factCounter == 0) { // Base case: 0! = 1
  6. printf("1\n");
  7. }
  8. else if (factCounter == 1) { // Base case: print 1 and result
  9. printf("%d = %d\n", factCounter, factValue);
  10. }
  11. else { // Recursive case
  12. printf("%d * ", factCounter);
  13. nextCounter = factCounter - 1;
  14. nextValue = nextCounter * factValue;
  15. /* Your solution goes here */
  16. PrintFactorial(nextCounter,nextValue);
  17. }
  18. }
  19. int main(void) {
  20. int userVal = 0;
  21. userVal = 5;
  22. printf("%d! = ", userVal);
  23. PrintFactorial(userVal, userVal);
  24. return 0;
  25. }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts