Solve C Recursive Case for Factorial Math Formula

  • Context:
  • Thread starter Thread starter ksepe
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
ksepe
Messages
5
Reaction score
0
I have got the recursive case... I cannot get the output to actually be the factorial (math formula), This is where i am struggling
Code:
#include <stdio.h>

void PrintFactorial(int factCounter, int factValue){
   int nextCounter = 0;
   int nextValue = 0;

   if (factCounter == 0) {            // Base case: 0! = 1
      printf("1\n");
   }
   else if (factCounter == 1) {       // Base case: Print 1 and result
      printf("%d = %d\n", factCounter, factValue);
   }
   else {                             // Recursive case
      printf("%d * ", factCounter);
      nextCounter = factCounter - 1;
      nextValue = nextCounter * factValue;
[B] int i=0;
      int result;
      for (i=1; i<factCounter-1; i++){
         printf("%d * ", factCounter-i);
         result=factCounter*(factCounter-i);
      }
      printf("1 = %d\n", result);
   }[/B]
}

int main(void) {
   int userVal = 0;

   userVal = 5;
   printf("%d! = ", userVal);
   PrintFactorial(userVal, userVal);

   return 0;
}
 
Last edited:
Physics news on Phys.org


Dear writer,

Thank you for sharing your progress and struggles with the recursive case for the factorial function. I understand that you are having difficulty getting the output to match the factorial formula. I would like to offer some advice and suggestions to help you with this problem.

Firstly, it is important to understand the recursive case for the factorial function. The factorial of a number n is defined as n! = n * (n-1) * (n-2) * ... * 1. This means that the factorial of a number is equal to that number multiplied by the factorial of the number one less than it.

In your code, you are correctly using the recursive case by calling the function again with the next counter and value. However, there are a few things that could be improved upon to get the correct output.

One suggestion is to use a variable to store the result of the factorial calculation. In your code, you are using the variable "result" to store the result of the multiplication, but it is only being updated once in the for loop. Instead, you can use this variable to store the overall result of the factorial calculation by multiplying it with the next value in each recursive call.

Another suggestion is to use a loop to print the "*" symbol between each number in the factorial formula. This will help to make the output match the formula more closely.

Finally, it is important to handle the base cases correctly. In your code, you have correctly handled the base case for 0! = 1, but the base case for 1! = 1 is not necessary. You can simply print the result in the recursive case when the counter reaches 1.

I hope these suggestions help you to solve your problem and improve your code. Keep up the good work and don't give up, as recursive functions can be tricky to understand and implement correctly. If you continue to have difficulties, don't hesitate to reach out for help. As scientists, we understand the importance of collaboration and asking for assistance when needed. Good luck!