Online JSON Formatter

Factorial Program in Java – Formula, Example and Explanation

factorial program in java example

The factorial of a number is an important mathematical concept widely used in programming, statistics, probability, and algorithm design. Learning how to create a factorial program in Java is a great way to understand loops, recursion, and problem-solving techniques.

In programming, factorial calculations are often used when working with permutations, combinations, and mathematical computations. This guide explains the factorial formula and demonstrates how to implement a factorial program in Java using different methods.

What is a Factorial?

A factorial is the multiplication of all positive integers from 1 up to a given number. The factorial symbol is represented by an exclamation mark (!). For example, the factorial of 5 is written as 5!.

5! = 5 × 4 × 3 × 2 × 1 = 120

Factorials grow extremely fast. Even relatively small numbers produce very large results. For instance, 10! equals 3,628,800. Because of this, programming languages often use special data types to store large factorial values.

Factorial Formula

The mathematical formula used to calculate factorial is:

n! = n × (n − 1) × (n − 2) × ... × 3 × 2 × 1

In this formula, the number n represents a positive integer. The multiplication continues until the value reaches 1.

0! = 1

This rule is essential in mathematics and is used in many combinatorial calculations.

Java Program to Calculate Factorial Using Loop

The most common way to calculate factorial in Java is by using a loop. The program multiplies numbers from 1 to the given number.

import java.util.Scanner;

public class FactorialExample {

    public static void main(String[] args) {

        int num;
        long factorial = 1;

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        num = sc.nextInt();

        for(int i = 1; i <= num; i++) {
            factorial = factorial * i;
        }

        System.out.println("Factorial of " + num + " = " + factorial);
    }
}

In this program, the user enters a number and the loop multiplies values from 1 up to that number. The result is stored in the variable named factorial.

Factorial Program in Java Using Recursion

Recursion is another method to calculate factorial. In recursion, a function repeatedly calls itself until a base condition is reached.

public class FactorialRecursion {

    static long factorial(int n) {
        if (n == 0)
            return 1;
        else
            return n * factorial(n - 1);
    }

    public static void main(String[] args) {

        int num = 5;

        System.out.println("Factorial of " + num + " = " + factorial(num));
    }
}

The recursive function keeps calling itself with smaller values until the number becomes zero. After reaching the base case, the results are returned back through the function calls.

Applications of Factorials

Factorials are widely used in mathematics, computer science, and statistics. One major use is in permutations and combinations, where factorial formulas determine how many possible arrangements exist in a set of items.

In probability theory, factorials help calculate the likelihood of events. They are also used in algorithms, data science calculations, and complex mathematical modeling.

Factorials are also useful when teaching programming concepts such as recursion, loops, and algorithm complexity. Understanding factorials builds a strong foundation for solving advanced computational problems.

Conclusion

The factorial of a number is a fundamental mathematical concept used in many programming and scientific fields. By learning how to write a factorial program in Java, developers can understand important concepts like loops, recursion, and algorithm design.

Whether you calculate factorials using loops or recursion, the core logic remains the same. Mastering this concept helps programmers develop stronger problem-solving skills and prepares them for more advanced programming challenges.