15+ Java Factorial Calculator Examples

This post contains a total of 15+ Hand-Picked Java Factorial Calculator Examples with Source Code. All the Factorial Calculators are made using Java Programming language.

You can use the source code of these programs for educational purpose with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Harshit Gupta

Made by Harshit Gupta. Simple java program to get the factorial of a number. Enter your number in line ‘int number=5;’. ( Source )

public class Program
{
    public static void main(String[] args) {
        
        int i,fact=1;
        int number=5;
        if (number < 0){
        System.out.println(" [Factorial for Negative Numbers isn't defined!]\n");
        number *= -1;
        }
        
        for(i=1;i<=number;i++){
           fact=fact*i; 
        }
        if (number == 0) {
           fact = 1;
        }
        System.out.println("Factorial of " +number +" is = " +fact);
    }
}

2. By Saori Liu

Made by Saori Liu. ( Source )



import java.util.Scanner;

public class Factorial{

    public static void main(String[] args) {
    int n,c,fact=1;
    
System.out.println("Welcome to the Factorial Calculator!!\nPlease enter a number:\n");
  
  Scanner in = new Scanner(System.in);
  n = in.nextInt();
  
  if(n<0){
      System.out.println("The number can't be negative!");
  }else{
      for(c=1;c<=n;c++)
      fact = fact*c;
    System.out.println("The Factorial of "+n+" is: "+fact);
  }
        
    }
}

3. By Mind To Machine πŸ’»πŸ•†

Made by Mind To Machine πŸ’»πŸ•†. This program can calculate the factorial of numbers even > 100. Enter the number to get factorial. ( Source )

import java.math.BigInteger;
import java.util.Scanner;

public class BigNumbersFactorialCalculator
{
 public static void main(String[] args)
	{
		System.out.println(getFactorial(new BigInteger(new Scanner(System.in).next())));
	}
	
	// calculates and returns the factorial of n
	static BigInteger getFactorial(BigInteger n)
	{
		BigInteger _0 = new BigInteger("0"), _1 = new BigInteger("1"); 
		
		return n.equals(_0)? _1 : n.multiply(getFactorial(n.subtract(_1)));
	}

}

4. By Mohammed Izhan Ali

Made by Mohammed Izhan Ali. ( Source )

import java.io.*;
import java.math.BigInteger;

public class Program

{
    public static void main(String args[])throws IOException
{
InputStreamReader input=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(input);
int n=Integer.parseInt(br.readLine());

BigInteger factorial = BigInteger.ONE;

for(int i = 1; i <= n; ++i)

{

// factorial = factorial * i;

factorial = factorial.multiply(BigInteger.valueOf(i));

}

System.out.printf("Factorial of %d = %d", n, factorial);


    }
}

5. By NkosiTheCoder

Made by NkosiTheCoder. ( Source )

import java.math.BigInteger;
import java.util.Scanner;

public class Program
{
    //Large factorial calculator
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        BigInteger f = new BigInteger("1");
        
        int val = input.nextInt();
        for (int i = 2; i <= val; i++){
            f = f.multiply(BigInteger.valueOf(i));
        }
        System.out.println("factorial: " + String.valueOf(f));
    }
}

6. By Hope

Made by Hope. Java program to get factorial of a number. ( Source )

public class Program {
    public static void main(String[] args) {
        int x;
        int y;int fact = 1;
        for(x=0;x<5;x++)
        {
            
            
            if(x==0)
            System.out.println("1");
            else {
            y = x;
            fact = 1;
                while(y>=1)
                {
                 
                   fact = fact * y;
                   y--; 
                }
                System.out.println(fact);
            }
        }
    }
}

7. By Agha Asad Khan

Made by Agha Asad Khan. ( Source )

import java.util.Scanner;

public class FactorialExample
{
    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
       int i;
      int fact=1; 
      int number;//this num calculate //factorial
      System.out.print("Required Number ");
      number=s.nextInt();
      for(i=1;i<=number;i++)
      {
         fact=fact*i; 
      }
      System.out.println("Factorial of " +number+" is: "+fact);
    }
}

8. By Amit Dubey

Made by Amit Dubey. Enter any number of which you want the factorial. ( Source )

import java.util.*;
public class Program
{
    public static void main(String[] args) {
        int fact=1;
        Scanner sc=new Scanner (System.in);
        int n=sc.nextInt();
        for(int i=1;i<=n;i++)
            fact*=i;
        System.out.println("Factorial of "+n+" = "+fact);
    }
}

9. By Priti

Made by Priti. ( Source )



import java.util.Scanner ;
public class Program
{
 static int factorial(int n){
     if(n==0||n==1){
         return 1;
     }
     else{
         return n*factorial(n:n-1);
     }
 }
    public static void main(String[] args) {
    Scanner sc=newScanner(System.in);
    x =sc.int();
    a = x;
    p =sc.factorial(a);
  //  p =sc.nextfactorial();
    System.out.println("the factorial of the number is:" + p);
        
    }
}

10. By Muiz Haruna

Made by Muiz Haruna. ( Source )

import java.util.Scanner;

public class Program
{
public static int factorial(int n){
    if (n==0 || n==1) return 1;
    
    else{
    return(n * factorial(n-1));
    }
}
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int n = input.nextInt();
    System.out.print("The Factorial of " + n + " is " + factorial(n));
        
    }
}

11. By Gustav RΓΆdel

Made by Gustav RΓΆdel. ( Source )

import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
        int i;
        int result = 1;
        
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        
        for(i = 0; i < x; i++){
           result *= (i+1); 
        }
        System.out.println("Factorial of "+ x +" is: "+ result);
    }
}

12. By Richard A. Blondell β˜•

Made by Richard A. Blondell β˜•. Java program to Calculates the factorial of a number (non-negative integer) entered through the keyboard. ( Source )

import java.util.Scanner;

public class Program
{
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("Factorial calculator");
		System.out.println("Enter a number: ");
		int x = scanner.nextInt();
		scanner.nextLine();
		factorial(x);
	}
	
	// The method validates that the entered number is not less than 0. In that case, it multiplies the value i times the variable fact, in a for loop that executes x times.
	public static void factorial (int x) {
	    int fact = 1;
	    if (x < 0) {
            System.out.println("Error! The number must be \ngreater or equal than zero.");
        } else {
            for (int i = 1; i <= x; i++) {
                fact *= i;
            }
            System.out.println("The factorial of " + x + " is " + fact + ".");
        }
        
    }
}

13. By SΓΈmeΓΈneβ„’

Made by SΓΈmeΓΈneβ„’. ( Source )

import java.util.Scanner;
public class Program
{
	public static void main(String[] args) {
int factorial = 0;
long result = 1;
		Scanner input = new Scanner(System.in);
		System.out.println("Enter an integer to get the factorial. ");
		if(input.hasNextInt()==false){
		    System.out.println("Please enter an integer");
input.close();
return;
		}else{factorial=input.nextInt();}
	for(int i = 1; i<=factorial; i++){
    result=result*i;
}
System.out.println(factorial+"! = "+result);
	}
}

14. By Demarri Dosunmu

Made by Demarri Dosunmu. ( Source )

import java.util.Scanner;

public class FactorialCalculator
{
	public static void main(String[] args) {
	    System.out.println("Please enter a number to factorialize.");
	    
	    Scanner numberInput = new Scanner(System.in);
		double numberx = numberInput.nextDouble();
		numberInput.close();
		
		int number = (int) numberx;
		
		Double factorial = 1.0;
		double x = 1;
		
		while (x<=number) {
		    factorial = factorial*x++;
		}

		    System.out.printf(number + " factorialized: %.0f\n", factorial);
		    if (factorial.isInfinite()) {
		    	System.out.println("Digit count: Infinity");
		    } else {
		    int length = String.valueOf(factorial).length() - 2;
		    System.out.println("Digit count: " + length);
	}
}
}

15. By Can

Made by Can. ( Source )

import java.util.Scanner;
public class Factorial_Calculator
{
static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args){
        int x = scanner.nextInt();
        int fact = 1;
        for (int i=1;i<=x;i++){
           fact = i * fact ; 
        
        }
  System.out.print(fact);
    }
}

16. By ARST

Made by ARST. A basic python factorial finder program. ( Source )

import java.util.Scanner;
public class Fact
{
    public static void main(String[] args) {
        Scanner s1=new Scanner(System.in);
        int n=s1.nextInt();
        int b=n;
        int i=1;
        if (n==0)
        System.out.print("n factorial = 1");
        else if (n<0)
        System.out.print("Error");
        else {
        while(1<=n){
           i*=n;
           n--;
        }
        System.out.print(b+" factorial = "+i);
        }
    }
}