Java Program to find factorial of a number


In this program, You will learn how to find factorial of a number in Java.


3! = 1 * 2 * 3

4! = 1 * 2 * 3 * 4


Example: How to find factorial of a number in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int n, i;
        long f = 1;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a Number :");
        n = sc.nextInt();

        for (i = 1; i <= n; i++) {
            f = f * i;
        }

        System.out.println("\nThe Factorial is :" + f);

    }
}

Output:

Enter a Number :4
The Factorial is :24