Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Need To generate the factorial of No Greater Than 10 In C

  1. #1
    Junior Member
    Join Date
    May 2002
    Posts
    17

    Need To generate the factorial of No Greater Than 10 In C

    Helllo Guy's
    I was thinking of this problem for the past one month. We have to generate the factoroial of say 100 using c and usual integer data type how can this be achived
    thanx in advance

  2. #2
    Senior Member
    Join Date
    Oct 2002
    Posts
    1,130
    int main(){
    int x=0;
    int y=100;
    int total;
    while(x<=y){
    total *= x;
    x++;
    }
    printf("The value is %i",total);
    return 0;
    }

    Might work. But I make no guarantees. Might be hard to calculate 100! with a 32 bit integer though. It's like 9.3*10^157 or something like that.

    You could also use a recursive algorithm

    int factorial(int x){
    static int total=0;
    total*=x;
    x--;
    factorial(x);
    return 0;
    }

    But I suck at writing recursive algorithms.

    And please stop asking us to do your homework for you. We have better things to do.
    Government is like fire - a handy servant, but a dangerous master - George Washington
    Government is not reason, it is not eloquence - it is force. - George Washington.

    Join the UnError community!

  3. #3
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Might work. But I make no guarantees. Might be hard to calculate 100! with a 32 bit integer though. It's like 9.3*10^157 or something like that.
    If it's really like 9.3*10^157 or something like that, then it WON'T work. The maximum unsigned 32 bit integer is 4.3 x 10^9. The maximum unsigned 64 bit integer is 1.8 x 10^19. You will need a (dynamically allocated) string to store the result.

    I remember my friend at univ wrote a program to calculate 200! using string. But I forget how. Don't care actually. What's the purpose anyway, other than doing your homework...

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  4. #4
    Junior Member
    Join Date
    Nov 2003
    Posts
    7
    sorry guys but you've got your math messed up over there.

    'total' will never go beyond zero if you use those routines.

  5. #5
    Just a Virtualized Geek MrLinus's Avatar
    Join Date
    Sep 2001
    Location
    Redondo Beach, CA
    Posts
    7,323
    vozhan, so what would you use?
    Goodbye, Mittens (1992-2008). My pillow will be cold without your purring beside my head
    Extra! Extra! Get your FREE copy of Insight Newsletter||MsMittens' HomePage

  6. #6
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    vozhan, Shriek never said it would work for sure:

    Originally posted here by Striek
    Might work. But I make no guarantees. Might be hard to calculate 100! with a 32 bit integer though. It's like 9.3*10^157 or something like that.
    Actually, it might go over 0, depending on how overflows are taken into account the integer would wrap around and start counting from 0 again (or a negative number like -(2 ^ 31) if you're using signed integers).
    Paul Waring - Web site design and development.

  7. #7
    Senior Member
    Join Date
    May 2002
    Posts
    344
    You could also use a recursive algorithm

    int factorial(int x){
    static int total=0;
    total*=x;
    x--;
    factorial(x);
    return 0;
    }
    No way! Recursion is defiently not the way to figure this type of program, you will run out of memory very quickly. If you really need help, PM me and ill show you but i am very busy at the moment because of finals so i wont have time for the next like week so i dont know if ill be much help...
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  8. #8
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    vozhan is correct. With that algorithm, total will never get past 0 because of the following:

    static int total=0;
    total*=x;
    If you look above, you will notice that 0 has been assigned to total to start, then total is timed by x. Because total is zero, it doesn't matter what x is:

    total x "new total"
    0 1 0
    0 2 0
    0 3 0

    If you set total to 1 to start with, it might be alright...but it should be much better to use something like a while or a for loop rather than writing a recursive algorithm, if only because you should make your code as readable as possible unless you are entering the obsfucated c code competition :P

    ac

    [edit]the columns are a bit messed up in the "table" above, but you get the idea[/edit]
    [edit]and you couldn't use a string to solve the problem simply because Hunt_guy said that he was to use the integer data type. If he was allowed to use a string, why not just use a long?[/edit]

  9. #9
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    One way to go about the solution would be to note the maximum size of an integer. Every time you reach this maximum size, you add one to a counter and start your "total" integer again (well not again, from wherever you got to). When you have worked out the whole factorial, you simply use output it to the screen:

    std::cout &lt;&lt; (counter*MAX_INT_SIZE)+total &lt;&lt; endl;

    or use printf (I don't want to make a fool of myself attempting to write it using printf, I never got used to the %s, %d, etc...never know which one to use :P

    Anyhow, that would be a simple way of doing it.

    ac

    [edit] I just realised that that would not be entirely correct in that it assumes that you would get exactly the max int value every time, but it is almost right :P [/edit]

  10. #10
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Erm, maybe you better ignore my last post. I've thought about it and I don't think that my maths is anywhere near correct . Hrrm...

    Must be too late at night.

    ac

Posting Permissions

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