In Java programming, recursion is a technique that allows a function to call itself during its execution.

Recursion is commonly used to solve problems that can be broken down into smaller, simpler problems.

One common problem that can be solved using recursion is to find the factorial of a number.

In this tutorial, we will discuss how to find the factorial of a number using recursion in Java.

Factorial of a number n is the product of all positive integers less than or equal to n.

It is denoted by n!. For example, the factorial of 5 is 5! = 5 × 4 × 3 × 2 × 1 = 120.

To find the factorial of a number using recursion, we can define a recursive function that calls itself with a smaller value until it reaches the base case, which is the factorial of 1.

Here is the Java code to find the factorial of a number using recursion:

public class FactorialUsingRecursion { public static void main(String[] args) { int num = 5; int factorial = factorial(num); System.out.println("Factorial of " + num + " is " + factorial); } public static int factorial(int n) { if (n == 1) { return 1; } else { return n * factorial(n - 1); } } }

In this code, we define a function called `factorial`

that takes an integer `n`

as input and returns its factorial.

The function checks if `n`

is equal to 1, which is the base case.

If it is, the function returns 1.

If not, the function calls itself with `n-1`

and multiplies the result with `n`

to get the factorial of `n`

.

In the `main`

function, we call the `factorial`

function with the input value `num`

(which is set to 5) and store the result in the `factorial`

variable.

Finally, we print the result using the `System.out.println`

function.

When you run this program, it will output the following:

Factorial of 5 is 120

In conclusion, finding the factorial of a number using recursion is a simple and elegant solution.

By breaking down the problem into smaller sub-problems, we can use recursion to find the factorial of any number.

The Java code provided above demonstrates this technique in action.