
10 Types of Programming Languages Every Coder should Know
Learn about different types of programming languages and how they are implemented in the real world.
A factorial of a non-negative integer n is the product of all positive integers less than or equal to n. it is represented by n!. For instance, the factorial of 5 is:
5! = 5×4×3×2×1=120
Why are factorials so important? Here are some fields in which the concept of factorial is used extensively:
Uses of Factorials in Mathematics
Uses of Factorials in Computer Science
The following article will examine five distinct approaches to writing a factorial program in Java and compare the time and space complexity of each approach.
Also Read: Java Tutorial
import java.util.Scanner;
public class FactorialForLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("Factorial of " + number + " = " + factorial);
}
}
Output:

import java.util.Scanner;
public class FactorialWhileLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
long factorial = 1;
int i = 1;
while (i <= number) {
factorial *= i;
i++;
}
System.out.println("Factorial of " + number + " = " + factorial);
}
}
Output:

import java.util.Scanner;
public class FactorialRecursion {
static int factorial(int n) {
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
int result = factorial(number);
System.out.println("Factorial of " + number + " = " + result);
}
}
Output:

When dealing with large numbers, the BigInteger class in the java.math package is our friend. It helps us avoid overflow issues.
import java.math.BigInteger;
import java.util.Scanner;
public class FactorialBigInteger {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
BigInteger factorial = BigInteger.ONE;
for (int i = 1; i <= number; i++) {
factorial = factorial.multiply(BigInteger.valueOf(i));
}
System.out.println("Factorial of " + number + " = " + factorial);
}
}
Output:

import java.util.Scanner;
public class FactorialTernary {
int factorial(int n) {
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
FactorialTernary obj = new FactorialTernary();
System.out.println("Factorial of " + number + " = " + obj.factorial(number));
}
}

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Let’s examine the time and space complexity of each technique we covered so that you can decide which approach to take into account while creating a factorial program in Java.
Understanding how long each method takes to run helps us choose the right one.
Space complexity tells us how much memory each method uses.
Let’s discuss a few common challenges that we should avoid when writing a factorial program in Java.
Also Read: Structure of Java Program
In this guide, we’ve walked through several ways to write a factorial program in Java. From for loops to recursion, and even using BigInteger for those massive numbers. Each method has its strengths and trade-offs. Choosing the right one depends on your specific needs—whether it’s minimising memory use or handling large values efficiently.
Updated on July 26, 2024

Learn about different types of programming languages and how they are implemented in the real world.

Explore 10 front-end development, including key languages, its advantages and disadvantages, and how it shapes user experience in web design and functionality.