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 53 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
| 8474 views |
  • submit to reddit

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:


IterativeOutput

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:
AddJar

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

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:
LibraryOutput

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

Comments

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


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.