Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello all,
I am trying to do a factorial program which can able to find factorial for large numbers also. I tried with long integer. I got correct result upto 12. I want to do it atleast for 25. Please suggest me a good code for the same.
Thnx in Advance
SURESH

Suresh-
You've posted here enough to know that we need more information and the code you've done so far before we'll help you much with homework.First and foremost, you need to tell us which language you're using. From the windows calculator, 25! is:
15511210043330985984000000
This is quite a bit bigger (840,000 bigger, in fact) than long can usually handle (I'm told it depends on the compiler, but isn't int 32 bit and long 64 bit?) If you're doing this in Java, look into using the BigInteger class, which can do this with arbitrarily large numbers.You will get slightly better results using an unsigned long integer, but even that won't be big enough to hold 25!.
Best of luck,
-SN

You know... this sounds like a tricky professor assignment, I miss college...
At any rate, next try calculating the execution sequence for 2000 Towers of Hanoi discs, I would like to compare answers :)
IR

Factorial algorythm...now that's simple.
IR, I am ashamed you did not even suggest it.
To create a factorial algorythm, you simply need to use a recursion function with the base case of 1.
The code would be something like this:
unsigned long CalcFactorial(unsigned long number)
{
if(number <= 1)
return 1;
else
return number CalcFactorial(number -1);
}That's it.
Chi Happens

Um...why did the multiplication symbol not appear?
the last line should read:
return number * CalcFactorial(number -1);Chi Happens

btw, with the following code, i can get up to 33! using Borland C++ Builder 5:
-------------------
#include <iostream>
#include <conio>
-------------------
using namespace std;unsigned long CalcFactorial(unsigned long);
unsigned long CalcFactorial(unsigned long number)
{
if(number <= 1)
return 1;
else
return number * CalcFactorial(number -1);
}int main(int argc, char* argv[])
{
cout << "33! = " << CalcFactorial(33) << endl;
getch();
return 0;
}
-------------------34, however, returns 0 (zero) because it overflows.
Chi Happens

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |