Java program to find factorial
This java program finds factorial of a number. Entered number is checked first if its negative then an error message is printed.
Java programming code
Download Factorial program class file.
Output of program:
You can also find factorial using recursion, in the code fact is an integer variable so only factorial of small numbers will be correctly displayed or which fits in 4 bytes. For large numbers you can use long data type.
Java program for calculating factorial of large numbers
Above program does not give correct result for calculating factorial of say 20. Because 20! is a large number and cant be stored in integer data type which is of 4 bytes. To calculate factorial of say hundred we use BigInteger class of java.math package.
Download Big Factorial class file.
We run the above java program to calculate 100 factorial and following output is obtained.
ConversionConversion EmoticonEmoticon