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 SNIPPETpublic 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,