MHB Solve C Recursive Case for Factorial Math Formula

  • Thread starter Thread starter ksepe
  • Start date Start date
AI Thread 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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top