Java Program to Find Factorial of a Number Using Recursion

This Java example code demonstrates a simple Java program to calculate factorial values using recursion and print the output to the screen. In this Java program, a function is defined that calls itself multiple times until the specified condition matches. This process is called Recursion, and the function is called the Recursive function.

Example:

public class Factorial {

    public static void main(String[] args) {
        //Variable definition and assignment
        int num = 5;

        //Call recursive function
        long factorial = multiplyNumbers(num);

        //Print the output
        System.out.println("Factorial of " + num + " = " + factorial);
    }

    public static long multiplyNumbers(int num)
    {
        if (num >= 1)
            return num * multiplyNumbers(num - 1);
        else
            return 1;
    }
}

Program Output:

Factorial of 5 = 120


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.