Register for 45 Day Coding Challenge by CodeStudio And Win Some Exciting Prizes

# Factorial Using Recursion in Java

Previously we developed the Java program to find the factorial using iterator. Now, we will develop the Java program to find factorial value using the recursion technique.

A function/method that contains a call to itself is called the recursive function/method. A technique of defining the recursive function/method is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.

Factorial of a number n is given by 1 * 2 * … * (n-1) * n and it’s denoted by n!
Example Factorial of 5= 5! = 5*4*3*2*1 or 1*2*3*4*5

Generally, Factorial of a number can be found using the for loop and while loop. But we can also use recursion technique to find the factorial of a given integer number. Here the problem of finding n! divide them into smaller problem as n! = n * (n-1)!

We know that
4! = 4*3*2*1
We can write it as,
4! = 4 * 3!
Similarly, we can write
3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
0! = 1

So, in general, we can say that factorial of a positive integer n is the product of n and factorial of (n-1).

``n! = n * (n-1)!``

Now, the problem of finding out factorial of (n-1) is similar to that of finding out factorial of n, but it is smaller in size. So, we have defined the solution of the factorial problem in terms of itself. We know that the factorial of 0 is 1 and similarly factorial of 1 is 1. This can act as the terminating condition or the base case.

The base case for finding factorial

``````factorial(0) = 1
Or,
factorial(1) = 1``````

General case for finding factorial

``factorial(n) = n * factorial(n-1)``

## Factorial program

``````import java.util.Scanner;

public class FactorialRecursion {

// Java recursive method to
// find factorial of a number
// using if-else statement
public static long findFactorial(long number) {
if(number == 0) return 1;
else return number*findFactorial(number-1);
}

public static void main(String[] args) {

// declare variables
int number = 0;
long result = 0;

//create Scanner class object to take input
Scanner scan = new Scanner(System.in);
// take input from end-user
System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// calculate factorial value
result = findFactorial(number);

// display result
System.out.println("Factorial = "+ result);

// close Scanner class object
scan.close();
}
}``````

The output for the different test-cases are:-

Enter an integer number:: 5
Factorial = 120

Enter an integer number:: 10
Factorial = 3628800

## Java factorial method using recursion in a single line

The ternary operator can be used to develop factorial method in a single line.

``````import java.util.Scanner;
public class FactorialRecursion {

// recursive Java method to
// find factorial of a number
// using ternary operator
public static long findFactorial(int n){
return (n==0) ? 1 : n*findFactorial(n-1);
}

public static void main(String[] args){

// declare variables
int number = 0;
long result = 0;

//create Scanner class object to take input
Scanner scan = new Scanner(System.in);
// take input from end-user
System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// calculate factorial value
result = findFactorial(number);

// display result
System.out.println("Factorial = "+ result);

// close Scanner class object
scan.close();
}
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!