DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Finding Factorial of a Number in Java

Finding Factorial of a Number in Java

Mohamed Sanaulla user avatar by
Mohamed Sanaulla
CORE ·
Feb. 01, 13 · Interview
Like (1)
Save
Tweet
Share
17.87K Views

Join the DZone community and get the full member experience.

Join For 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

Google Guava Java (programming language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Taming Cloud Costs With Infracost
  • Express Hibernate Queries as Type-Safe Java Streams
  • Observability vs Monitoring Use Cases
  • Last Chance To Take the DZone 2023 DevOps Survey and Win $250! [Closes on 1/25 at 8 AM]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: