Java program to find the factorial of a number

First create a file Factorial.java and write below program in it. Now First compile program by using below command.

javac Factorial.java

Now run the Java program using following command.

java Factorial

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

        int factorial = 1,num = 7;
        for(int i = 1; i <= num; i++){
            factorial = factorial * i;
        }
        System.out.println("Factorial of " + num + " is " +factorial);
    }
}

Output

Factorial of 7 is 5040

Leave a Reply