Java Program to Find Factorial of a Number using FOR LOOP

In this tutorial we will write a Java program to find the factorial of a number using for loop. Factorial of a number is given as –

n! = n*(n-1)! till 1.
E.g. 4! = 4 x 3 x 2 x 1
5! = 5 x 4 x 3 x 2 x 1
6! = 5 x 5 x 4 x 3 x 2 x 1

We will be using For Loop to find the factorial in this program.

Java program to Find Factorial of a Number –
public class JavaApplication4 {
    public static void main(String[] args) {
     
        int num = 4;
        int factorial =1;
        for(int i = num; i>0; i--)
        {
            factorial = factorial *i;
        }
        System.out.println("Factorial is: "+factorial);
      
    }
}
Output
Factorial is: 24
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *