Over a million developers have joined DZone.

Finding Factorial of a Number in Java

· Java Zone

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}