Thread: New program need help with factorials

  1. #1
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56

    New program need help with factorials

    I need to know how to find the factorial (5!=125) of a variable and, using it and for loops, put all of the possible combinations in an array.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by AdamLAN
    I need to know how to find the factorial (5!=125) of a variable
    Use a loop to calculate the factorial.

    Quote Originally Posted by AdamLAN
    and, using it and for loops, put all of the possible combinations in an array.
    Huh? Combinations of what into the array?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    factorial of x is x*factorial(x-1), and factorial 0 is 1
    have fun... I'd sugest a cicle instead of recursion... recursion does functions call, which have a big overhead and use lots of stack.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    38

    Lightbulb

    Quote Originally Posted by AdamLAN
    5!=125
    Close... 5! = 120.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    AdamLAN,

    Do you know what a factorial is? Can you do it by hand or on a calculator? ... I mean a calculator without a factorial button.

    5! means 1 x 2 x 3 x 4 x 5 = 120. (Of course, that's NOT C++ because x does NOT mean multiply in C++.)

    Step 1
    If you can't figure-out the whole program, how about starting with a loop that generates the sequential integers 1 thru n: 1, 2, 3, 4...

    Step 2
    When that's working, add the code to calculate the factorials.

    Step 3
    When you get that working, add the array stuff.

    I assume that by "all the possible combinations" you mean this all of the factorials up to n: 1, 2, 6, 24...
    Last edited by DougDbug; 05-20-2005 at 01:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Program to calculate factorials
    By forzamilan in forum C++ Programming
    Replies: 1
    Last Post: 09-22-2001, 10:24 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM