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

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

1. 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

2. 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.

3. 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>

4. sorry guys but you've got your math messed up over there.

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

5. vozhan, so what would you use?

6. 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).

7. 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...

8. 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

the columns are a bit messed up in the "table" above, but you get the idea[/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. 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

 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. 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

Page 1 of 3 123 Last

Posting Permissions

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