Java Program to Find Factorial Using Recursion

1. Introduction

The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. It's denoted by n! and plays a significant role in mathematics and computer science, especially in permutations and combinations. This blog post will demonstrate how to calculate the factorial of a number using recursion in Java, a fundamental concept in programming that involves a function calling itself.

2. Program Steps

1. Define a recursive method to calculate the factorial.

2. Read the number for which the factorial is to be calculated from the user.

3. Call the recursive method with the user input.

4. Display the factorial of the given number.

5. Handle cases where the input is less than 0, as factorials for negative numbers are not defined.

3. Code Program

import java.util.Scanner;

public class FactorialUsingRecursion {
    public static void main(String[] args) {
        // Creating a Scanner object to read input
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a non-negative integer:");
        int number = scanner.nextInt(); // Reading the number

        // Checking if the number is non-negative
        if (number >= 0) {
            long factorial = factorial(number); // Calculating the factorial
            System.out.println("Factorial of " + number + " is: " + factorial);
        } else {
            System.out.println("Factorial is not defined for negative numbers.");
        }

        scanner.close(); // Closing the scanner
    }

    // Recursive method to calculate factorial
    public static long factorial(int n) {
        if (n <= 1) { // Base case: factorial of 0 or 1 is 1
            return 1;
        } else {
            return n * factorial(n - 1); // Recursive call
        }
    }
}

Output:

Enter a non-negative integer:
5
Factorial of 5 is: 120

Explanation:

1. The program begins by importing the Scanner class for reading user input.

2. A Scanner object is created, and the user is prompted to enter a non-negative integer. This integer is read and stored in the number variable.

3. Before calculating the factorial, the program checks if the input number is non-negative. Factorials are only defined for non-negative integers.

4. The factorial method is defined as a recursive function. It takes an integer n as input and returns its factorial. The base case of the recursion is when n is 0 or 1, in which case the method returns 1. For all other values of n, the method returns n multiplied by the factorial of n-1.

5. This recursive approach simplifies the process of calculating the factorial by breaking it down into simpler sub-problems. The multiplication accumulates during the return phase of the recursion, ultimately yielding the factorial of the original number.

6. After computing the factorial, the result is printed to the console.

7. Finally, the Scanner object is closed to prevent resource leaks.

Comments