MHB Solve C Recursive Case for Factorial Math Formula

  • Thread starter Thread starter ksepe
  • Start date Start date
Click For Summary
The discussion focuses on troubleshooting a C program designed to calculate the factorial of a number using recursion. The main issue is that the output does not correctly reflect the factorial formula. Key points include the importance of properly implementing the recursive case, where the factorial of a number n is defined as n! = n * (n-1)!. Suggestions for improvement include storing the cumulative result of the factorial calculation in a variable that updates with each recursive call, ensuring the correct printing of the multiplication symbols between numbers, and simplifying the base case handling. The advice emphasizes the need for clarity in output and correct recursive logic to achieve the desired factorial result.
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:
Technology 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!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
2K
Replies
14
Views
3K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K