w3resource

Factorial Calculator in Java: Loops and Recursion


Java Project - Factorial Calculation:

Factorial Calculator :

Calculate the factorial of a number using loops or recursion.

This project calculates the factorial of a given number using either loops or recursion. The factorial of a number is the product of all positive integers up to that number.

Input: A number.
Output: Factorial of the number.

Example:

  • Input: 5
  • Output: 120

Solution 1: Factorial Calculation using Loops

Code:

import java.util.Scanner;

public class FactorialUsingLoop {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Taking user input
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        
        // Initializing factorial value to 1
        int factorial = 1;
        
        // Loop to calculate factorial
        for (int i = 1; i <= num; i++) {
            factorial *= i;
        }
        
        // Displaying the result
        System.out.println("Factorial of " + num + " is: " + factorial);
        
        scanner.close(); // Closing the scanner resource
    }
} 

Output:

Enter a number:  6
Factorial of 6 is: 720

Explanation :

  • Input and Scanner: The program takes an integer input from the user.
  • Factorial Initialization: A variable factorial is initialized to 1 to store the result.
  • For Loop:| The loop runs from 1 to the input number, multiplying the factorial variable by each value of the loop counter.
  • Display Result: After the loop ends, the factorial result is displayed to the user.
  • Close Scanner: The scanner is closed to free up system resources.

Solution 2: Factorial Calculation using Recursion

Code:

 import java.util.Scanner;

public class FactorialUsingRecursion {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Taking user input
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        
        // Calling recursive function to calculate factorial
        int result = calculateFactorial(num);
        
        // Displaying the result
        System.out.println("Factorial of " + num + " is: " + result);
        
        scanner.close(); // Closing the scanner resource
    }
    
    // Recursive function to calculate factorial
    public static int calculateFactorial(int n) {
        if (n == 0 || n == 1) { // Base case: factorial of 0 or 1 is 1
            return 1;
        } else {
            return n * calculateFactorial(n - 1); // Recursive call
        }
    }
} 

Output:

 Enter a number:  7
Factorial of 7 is: 5040

Explanation:

  • Input and Scanner: The program takes an integer input from the user.
  • Recursive Function: The calculateFactorial() method is a recursive function that calls itself until the base case (n == 0 or n == 1) is reached.
  • Base Case: The factorial of 0 or 1 is returned as 1.
  • Recursive Call: For other values of n, the function multiplies n by the factorial of n-1.
  • Display Result: After the recursive function completes, the result is displayed to the user.
  • Close Scanner: The scanner is closed to free up system resources.

Java Code Editor:




Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-factorial-calculator.php