In this post, you will learn how to use a recursive method to calculate the factorial of a number in Java. A recursive method is one that executes itself recursively. Using Java, this recursive method is used to find the factorial of a given integer number.

Java Program

package in.yawin;

public class FactorialRecursive {
	public static void main(String[] args) {
		int number = 6;
		int fact = fact(number);
		System.out.println("Factorial of " + number + " = " + fact);
	}

	public static int fact(int number) {
		if (number >= 1)
			return number * fact(number - 1);
		else
			return 1;
	}
}

Output

Factorial of 6 = 720

Explanation

The java programme will use a recursive method to find the factorial of a given number. To find the factorial of an integer number, the fact method is used recursively. If the function’s parameter value is 1, the fact method will return 1. Otherwise, it will decrement the value and recursively call the same method. The recursive method’s output will be multiplied by the method’s parameter value. In this manner, the java programme will use a recursive method to find the factorial of a given number.

Leave a Reply