# factorial program: 10! ??

• 10-21-2012
Iamanewbie
factorial program: 10! ??
I'm writing a program that's supposed to compute and print the factorials of the numbers 1-10, but when I compile and run it, it displays 0 where the factorial of ten should be. I messed with my loops and changed it so it did 1-11, and it printed zero for both numbers over one digit. I'm using %lu in the print statement instead of %d, and I have no idea what's causing this problem!
here's the code:
Code:

```#include <stdio.h> #include "simpio.h" #include "genlib.h" main() {     int original, factorial, i;     original=1;     factorial=1;     i=1;     for (original>=1; original<=10; original++)     {           for (i=1; i<=original; i++)           {               factorial=factorial*i           }           printf ("%d  %lu\n", original, factorial);     }     getchar(); }```
• 10-21-2012
iMalc
Since 'original' is the number that you are calculating to factorial for each time, 'factorial' will need to start at 1 each time.
You'll find that your answers are currently comming out far too big because of that mistake, and you're probably getting integer overflow.

Of course if you're clever, you can instead avoid the inner loop entirely, using the result of the previous factorial to generate the next factorial.
• 10-21-2012
Iamanewbie
Thanks! I solved the problem by defining 'factorial' as 1 again before the start of the inner loop. I hadn't thought of that!
I hadn't thought about avoiding the inner loop, but that would be interesting. I don't think I want to try to do that now, because I was incredibly frustrated with this program and I really don't want to look at it right now. I'll keep it in mind for another time, though.