Factorial Program in Java

You are currently viewing Factorial Program in Java

Hello reader! I’m going to teach the tutorial about Factorial Program in Java to you. Basically, We will talk about what is the factorial? and what are the possibilities to perform a task here? Let’s move on to this tutorial.

What is the Factorial(!)?

The product of all non-negative integers that are smaller than or equal to n is called the factorial(!) of n. Basically, The factorial of “0” is always “1”.

For example, If we can assign the n value as “6” then the factorial of n will be like: (6!) = (6*5*4*3*2*1) = 720.

Factorial Program in Java Using For Loop

public class Factorial {

    public static void main(String[] args) {

        int num = 12;
        long factorial = 1;
        for(int i = 1; i <= num; ++i)
        {
            // factorial = factorial * i;
            factorial *= i;
        }
        System.out.printf("Factorial of %d = %d", num, factorial);
    }
}  

Execution

Firstly, We have to declare the input number with the respective variable. After executing the program, The for loop iterates to find the factorial(product) from the input number to 1 in descending order respectively. Finally, The output will display on the screen using the system.out.printIn() function.

Output

The output of the factorial program in java will show below for your reference.

Factorial of 12 = 479001600

Using While Loop

Here, We can use the while loop instead of the for loop to perform a task.

public class Factorial {

    public static void main(String[] args) {

        int num = 9, i = 1;
        long factorial = 1;
        while(i <= num)
        {
            factorial *= i;
            i++;
        }
        System.out.printf("Factorial of %d = %d", num, factorial);
    }
}

Execution

Here, The while loop rather than the for loop iterates to calculate the product of a given number from itself to 1 in reversed order. Otherwise, The execution process is the same.

Output

Below, You will see the reference output of the factorial program in java.

Factorial of 9 = 362880
Read Also : Arithmetic Operators in Java | Arithmetic Operations in Java

Note

If you want to find Factorial Program in Javascript, and Factorial Program in PHP then read those articles by clicking on the above link.

Using Big Integer

Instead of using all the above methods, We can use the big integer method to do a task here.

import java.math.BigInteger;

public class Factorial {

    public static void main(String[] args) {

        int num = 40;
        BigInteger factorial = BigInteger.ONE;
        for(int i = 1; i <= num; ++i)
        {
            // factorial = factorial * i;
            factorial = factorial.multiply(BigInteger.valueOf(i));
        }
        System.out.printf("Factorial of %d = %d", num, factorial);
    }
}

Explanation

Initially, We have to import the java.math.BigInteger() class into the program. Here, We will use the for loop along with the BigInteger.valueOf() function to perform a task with a bigger input number. Otherwise, The program execution is the same. Finally, The system.out.printIn() will print the result on the screen.

Output

The output of the factorial program in java will be given below for your reference.

Factorial of 40 = 815915283247897734345611269596115894272000000000

Final Thoughts

I hope this article helps you to find the factorial of numbers using the Java Program. If you face any issues please let me know via the comment section. Share this article with other Java program developers via social networks. Thank You!

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply