Java Program to find factorial of a number using recursion


In this program, You will learn how to find factorial of a number using recursion in Java.


3! = 1 * 2 * 3

4! = 1 * 2 * 3 * 4


Example: How to find factorial of a number using recursion in Java

Run it Now
import java.util.Scanner;

class Main {

    int findFactorial(int num) {
        if (num == 0) {
            return 1;
        }
        num = num * findFactorial(num - 1);

        return num;
    }


    public static void main(String[] args) {
        int num, f;
        Scanner sc = new Scanner(System.in);
        Main obj = new Main();

        System.out.print("Enter a number : ");
        num = sc.nextInt();

        f = obj.findFactorial(num);
        System.out.println("\nFactorial is : " + f);

    }
}

Output:

Enter a number : 4
Factorial is : 24