Java Program to Find Factorial of a Number Using Recursion

In this Java program, you’ll learn how to Find Factorial of a Number Using Recursion using the Java programming language. 

How to Find Factorial of a Number Using Recursion in JAVA? 

Example 1: 

RUN CODE SNIPPET
public class Main 
{ 
    public static void main(String[] args) 
     { 
        int num = 5; 
        long factorial = multiplyNumbers(num); 
        System.out.println("Factorial of " + num + " = " + factorial); 
    } 
    public static long multiplyNumbers(int num) 
    { 
        if (num >= 1) 
            return num * multiplyNumbers(num - 1); 
        else 
            return 1; 
    } 
}

OUTPUT 

Factorial of 5 = 120

From the main() function the multiplyNumbers() is called and an argument 5 is passed

To get the results 5 is multiplied to the results of multiplynumbers() where 4(num-1) is passed. It is called as recursive call.  

The recursion will end when the num is less than 1, 

Share:

Leave A Reply

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

You May Also Like

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024