Thread: factorial program

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    11

    factorial program

    hi, i have a simple problem with my factorial program.

    I can't seem to get my .exe to stay open or when i do i can't input my numbers correctly. Any help with this?

    Code:
    #include <stdio.h>
    
    void factorial (long number, long *result);
    
    int main (int argc, char *argv[])
    {
        long argument, result;
        
        if (argc != 2)
        {
                 printf("factorial [long]\n");
                 return(0);
        }
        
        sscanf(argv[1], "%ld", &argument);
        
        factorial(argument, &result);
        
        printf("%1d! = %1d\n", argument, result);
        
        getchar();
    }
    
    void factorial (long number, long *result)
    {
         long temp;
         
         if (number < 2)
            *result = 1;
         else
         {
             factorial (number - 1, result);
             *result = number * (*result);
         }
    }
    I know this is not the easiest way to do factorial but just ignore that. Also im new to C programming, so thanks for your patience and help.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Works okay for me. You have a couple of minor mistakes:
    Code:
     printf("%1d! = %1d\n", argument, result);
    1 should be l.

    And main() should always return an int.

    Your problem has to do with your IDE, I think. What are you using?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    im using dev-c++ to compile, then im runnning the .exe from the same root folder as my c program

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Not a windows or dev-c++ user, sorry. But you might want to clarify this:

    i can't input my numbers correctly
    Since you aren't "inputting" anything, you just have one command line arg.

    Another useful thing is to use fprintf(stderr) to debug, eg:
    Code:
        if (argc != 2)
        {
                 printf("factorial [long]\n");
                 return(0);
        }
        fprintf(stderr, "arg: %s\n", argv[1]);
    The reason for using fprintf(stderr) rather than printf is that stdout is buffered and the output may not be flushed in sync with execution, whereas stderr is. This is a problem with debugging since the program can crash before the stdout buffer is flushed. You can also use:

    Code:
    printf(...); fflush(stdout);
    fflush() makes sure the output is done immediately.
    Last edited by MK27; 03-28-2010 at 07:53 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Works fine for me (using Code::Blocks, but I doubt it makes any difference).

    Just checking, but you do know that you have to run it from the command line...right? If you just click it you won't be supplying any input to it, as your program takes its input from the command line. The arguments to main( int argc, char *argv[] ) are used to access command line parameters that are supplied when the program is invoked.

    In Dev-Cpp there is an option under the menu "Execute" which says "Parameters...". Type the number that you want the factorial of in there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Factorial program
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2007, 03:01 AM
  4. Writing code for a program in C
    By Sure in forum C Programming
    Replies: 7
    Last Post: 06-11-2005, 01:33 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM