# Thread: Need Programming Homework Help Now!

1. ## Need Programming Homework Help Now!

ok guys... simple program but i dont have time. I just need a program writen in C that calculates the factorial of a number!

kinda like this
Code:
```main()
{
int num;
scanf("%d", &num);
factorial(num);
printf("%d", num);
}

int factorial(int num)
{
int total;
if(num = 1){return 1;}
total = num*factorial(num-1);
}```
The program is due tonight, any and all help is greatly appreciated! Thanks in advance.

2. Although factorial calculation is the classic example used for recursion, it is actually a very poor implementation due to the calculation intensity once numbers get very large. Most efficient implementations use a lookup table for smaller numbers (the last time I remember this was like up to 27 or something, but that was a few years ago), and then use a logarithmic function to estimate values that are above this (since the number of significant digits that can be used can be returned by such a function).

However, as long as you have a properly recursive compiler, your example looks like it should work.

3. The program MUST be recursively excecuted and it MUST check for overflow... which btw happens after 13 factorial on a 32 bit computer.
the one i just wrote returns 1 for any number... and doesn't check for overflow.

4. One note - if you can use a long instead of an int, you won't overflow as quickly.

5. Originally posted by Markel
One note - if you can use a long instead of an int, you won't overflow as quickly.

must use integer. Just following instructions on the lab, not making this stuff up

6. Can you just put a check in factorial for num > 13 (print error and return 0 or something)?

7. Originally posted by Markel
Can you just put a check in factorial for num > 13 (print error and return 0 or something)?
And i quote: "Since C is a portable language the factorial program should be able to run on ANY machine and hence should find overflow in ANY machine."

8. Ah, I'm beginning to get a better idea what's up. Hmm -- perhaps you could do something with MAXINT that would help.

9. whats maxint?

10. In most C implementations that I have seen MAXINT is a pre-defined constant equal to the largest positive integer value.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•