I have been in the software development industry since 2010, working on enterprise product development using ADF. I am usually keen on learning about software design and emerging technologies. You can find me hanging around in the JavaRanch Forums where I am one of the moderators. Apart from Java, I am fascinated by the ease of use and simplicity of Ruby and Rails. Mohamed is a DZone MVB and is not an employee of DZone and has posted 34 posts at DZone. You can read more from them at their website. View Full User Profile

# Finding Factorial of a Number in Java

02.01.2013
| 2923 views |

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;
while(true){
System.out.println("Enter the number for finding factorial");

// Read the number for finding factorial
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;

public class FactorialGuavaTest {

public static void main(String[] args) {
int number;
while(true){
System.out.println("Enter the number for finding factorial");
System.out.println("The factorial is: "+BigIntegerMath.factorial(number));
}

}

}```

The output for the above code is:

Published at DZone with permission of Mohamed Sanaulla, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

### sun east replied on Fri, 2013/02/01 - 9:38am

One line in Scala:

`def factorial(n: Int) = BigInt(1) to n product`

Test:
```scala> (21 to 30) foreach (n => printf("The factorial of %d is %d\n", n,factorial(n)))
The factorial of 21 is 51090942171709440000
The factorial of 22 is 1124000727777607680000
The factorial of 23 is 25852016738884976640000
The factorial of 24 is 620448401733239439360000
The factorial of 25 is 15511210043330985984000000
The factorial of 26 is 403291461126605635584000000
The factorial of 27 is 10888869450418352160768000000
The factorial of 28 is 304888344611713860501504000000
The factorial of 29 is 8841761993739701954543616000000
The factorial of 30 is 265252859812191058636308480000000

scala> (0 to 3) foreach (n => printf("The factorial of %d is %d\n", n,factorial(n)))
The factorial of 0 is 1
The factorial of 1 is 1
The factorial of 2 is 2
The factorial of 3 is 6
```