HomeJavaJava Program to Find Factorial of a Number Using Recursion

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

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022