Factorial Program in Java

Factorial Program in Java: Factorial is the mathematical term which basically means the product of a number in descending order until it reaches 1. It is represented by n!.

Want to learn Java? Check here.

An example for calculating the Factorial of a number is:

Let us assume the value of n is 5. We want to calculate the factorial of 5.

5! = 5*4*3*2*1 = 120.

Therefore, the factorial of 5 is 120.

Factorial Program in Java

Let’s now focus on the Factorial Program in Java.

Example 1: Factorial Program in Java without Recursion

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// Create a Scanner object to take user input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number
System.out.print("Enter a number: ");

// Read the user's input as an integer
int number = scanner.nextInt();

// Close the Scanner since we no longer need it
scanner.close();

// Calculate the factorial of the entered number
long factorial = calculateFactorial(number);

// Display the result
System.out.println("Factorial of " + number + " is " + factorial);
}

// function to calculate the factorial without recursion
public static long calculateFactorial(int n) {
int fact = 1; // variable which will store the factorial value
int i = 1; // used for iterating the while loop
while (i<=n) {
fact = fact*i;
i=i+1;
}

return fact;
}
}

The output of the above Code Snippet will be:

Factorial Program in Java without Recursion

Example 2: Factorial Program in Java with Recursion

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// Create a Scanner object to take user input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number
System.out.print("Enter a number: ");

// Read the user's input as an integer
int number = scanner.nextInt();

// Close the Scanner since we no longer need it
scanner.close();

// Calculate the factorial of the entered number
long factorial = calculateFactorial(number);

// Display the result
System.out.println("Factorial of " + number + " is " + factorial);
}

// Method to calculate the factorial of a number using recursion
public static long calculateFactorial(int n) {
// Base case: If n is 0 or 1, the factorial is 1
if (n == 0 || n == 1) {
return 1;
}
// Recursive case: Factorial of n = n * factorial of (n-1)
else {
return n * calculateFactorial(n - 1);
}
}
}

The output of the above Code Snippet will be:

 

Factorial Program in Java with Recursion

Example 3: Factorial Program in Java with For Loop

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// Create a Scanner object to take user input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number
System.out.print("Enter a number: ");

// Read the user's input as an integer
int number = scanner.nextInt();

// Close the Scanner since we no longer need it
scanner.close();

// Calculate the factorial of the entered number
long factorial = calculateFactorial(number);

// Display the result
System.out.println("Factorial of " + number + " is " + factorial);
}

// function to calculate the factorial without recursion
public static long calculateFactorial(int n) {
int fact = 1; // variable which will store the factorial value
for (int i=1; i<=n; i++) {
fact = fact*i;
}

return fact;
}
}

The output of the above Code Snippet will be:

 

Factorial Program in Java with For Loop

Check, How to write the Prime Number Program in Java.

Example 4: Factorial Program in Java without Functions

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// Create a Scanner object to take user input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number
System.out.print("Enter a number: ");

// Read the user's input as an integer
int number = scanner.nextInt();

// Close the Scanner since we no longer need it
scanner.close();

// variable for calculating the factorial of a number
int factorial = 1; 

for (int i =1; i<=number; i++) {
factorial = factorial*i;
}

// Display the result
System.out.println("Factorial of " + number + " is " + factorial);
}

}

The output of the above Code Snippet will be:

Factorial Program in Java without Functions

Conclusion

Factorial Program in Java is one of the most important programs, as it can be written in 2 special manners one with recursion and one without recursion. In this article, both the important ways are covered, and also how to do the same with for loop is also executed.