Thread: factorial program: 10! ??

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    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();
    }
    thanks in advance!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Factorial Program Not Printing Answer!?
    By drewtoby in forum C Programming
    Replies: 4
    Last Post: 04-20-2011, 09:27 PM
  2. Factorial program problem
    By fredz0003 in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2010, 11:29 PM
  3. factorial program
    By emblem4ever in forum C Programming
    Replies: 4
    Last Post: 03-29-2010, 04:44 AM
  4. Factorial Computation Program
    By ts9818a in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2008, 07:01 PM
  5. Factorial program
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2007, 03:01 AM

Tags for this Thread