Finding factorial of a number in Java
If you have tried to find a factorial using int as the data type, its most likely that you will soon get wrong outputs because the factorial value overflows the size of the int. One approach to over come is to use java.math.BigInteger to perform the factorial calculations and write a iterative/recursive method to find the factorial. Another approach would be to use com.google.common.math.BigIntegerMath provided by Google Guava API. I will show how to use both of these approaches in the post below:
Iterative based factorial calculation using BigInteger
import java.math.BigInteger;
import java.util.Scanner;
public class FactorialTest {
public static void main(String[] args) {
int number;
Scanner reader = new Scanner(System.in);
while(true){
System.out.println("Enter the number for finding factorial");
// Read the number for finding factorial
number = reader.nextInt();
if (number == -1){
break;
}
System.out.println("The factorial is: "+factorial(number));
}
}
/**
* Obtaining the factorial of a given number
*/
public static BigInteger factorial(int number){
//Note that we have used BigInteger to store the factorial value.
BigInteger factValue = BigInteger.ONE;
for ( int i = 2; i <= number; i++){
factValue = factValue.multiply(BigInteger.valueOf(i));
}
return factValue;
}
}
The output for the above code:

Google Guava API to find factorial:
I have used Eclipse to execute these samples and for using Google Guava you need to add the Google Guava library jar into the classpath of your project as shown below:

and after adding the jar you would see it being visible in the referenced libraries:

com.google.common.math.BigIntegerMath has a method factorial() which accepts a int value and returns factorial which would be of BigInteger type:
import java.util.Scanner;
import com.google.common.math.BigIntegerMath;
public class FactorialGuavaTest {
public static void main(String[] args) {
int number;
Scanner reader = new Scanner(System.in);
while(true){
System.out.println("Enter the number for finding factorial");
number = reader.nextInt();
System.out.println("The factorial is: "+BigIntegerMath.factorial(number));
}
}
}
The output for the above code is:

Related posts:
- Using JAXB to generate Java Objects from XML document Tweet Quite sometime back I had written about Using JAXB...
Related posts brought to you by Yet Another Related Posts Plugin.
Connect to us …
Archives
- May 2013 (8)
- April 2013 (6)
- March 2013 (6)
- January 2013 (5)
- November 2012 (2)
- September 2012 (1)
- July 2012 (5)
- June 2012 (1)
- May 2012 (4)
- April 2012 (7)
- March 2012 (2)
- February 2012 (4)
- December 2011 (2)
- November 2011 (4)
- October 2011 (2)
- September 2011 (1)
- August 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (1)
- April 2011 (1)
- February 2011 (4)
- December 2010 (3)
- November 2010 (2)
- September 2010 (2)
- August 2010 (3)
- May 2010 (2)
- March 2010 (6)
- December 2009 (1)
- November 2009 (3)
- July 2009 (6)
- June 2009 (3)
- May 2009 (1)
- April 2009 (6)
- March 2009 (1)
- January 2009 (1)
- December 2008 (8)
- November 2008 (5)
- October 2008 (6)
- September 2008 (4)
- August 2008 (8)
- July 2008 (19)
- June 2008 (29)
- May 2008 (27)
- April 2008 (11)
- March 2008 (8)
- February 2008 (22)
- January 2008 (3)
Send To Readmill
Send to ReadmillRecent Posts
- How to create ADF TreeTable programmatically?
- Book review: The Object-Oriented Thought Process
- How to show links in ADF Messages
- Runtime Polymorphism in Java
- Understanding RowKey values in ADF TreeTable
- Train Wreck Pattern – A much improved implementation in Java 8
- Converting a List into comma separated value string in Java
- A simple application of Lambda Expressions in Java 8
- Template Method Pattern- Using Lambda Expressions, Default Methods
- First look at Learning Play! Framework 2
Disclaimer
Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for your support.









