➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
In this post, we will develop the Factorial program in Java. We will see what are the different ways to find the factorial of a number in Java.
In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example:- The factorial of 4= 4! = 4*3*2*1 or 1*2*3*4 = 24
Factorial (n) =
1 * 2 * ... * (n-1) * n
Or,
n * (n-1) * ... * 2 * 1
The factorial of a negative number doesn’t exist. The value of 0! is 1, according to the convention for an empty product.
Procedure to find the factorial of a number in Java,
1) Take a number
2) Declare a temporary variable fact
and initialize it with 1. long fact = 1;
3) Take an iterator variable i
, starting from 1
4) Multiply fact variable and iterator variable. Store the result into fact variable. fact = fact * i;
5) Increase the iterator variable by 1.
6) Repeat 4 and 5 steps until the iterator variable is less than the given number.
import java.util.Scanner;
public class Factorial {
public static long findFactorial(int number){
// declare variable
long fact = 1;
for(int i=1; i<=number; i++) {
fact = fact * i;
}
return fact;
}
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 integer number:");
number = scan.nextInt();
// Find the factorial of
// the given number
result = findFactorial(number);
// display result
System.out.println("Factorial = "+ result);
// close Scanner class object
scan.close();
}
}
The output for different test-cases:-
Enter an integer number:: 5
Factorial = 120
Enter an integer number:: 4
Factorial = 24
It was a basic Java program to calculate factorial of a given number, which calls findFactorial() method to calculate the factorial value. The findFactorial method uses for loop and increment operator.
The for loop calculates the factorial of number 5 as = 1*2*3*4*5 because it uses increment operator. In place of increment operator, we can also use decrement operator, then it will calculate the factorial value as 5! = 5*4*3*2*1
The factorial method in Java using for loop and decrement operator,
public static long findFactorial(int number) {
// declare variable
long fact = 1;
for(int i=number; i>1; i--) {
fact = fact * i;
}
return fact;
}
Factorial for Large Values
The store the factorial value instead of int data type we are using a variable of long data type because the factorial value will be a larger number. But for a large number (like 50, 100) long data type is also not enough to store those values. So, we need to use a variable of BigInteger type which is defined in java.math package (not java.lang.Math class).
To multiply we can’t use the multiplication (*) operator because it is an object of BigInteger class so, we should use multiply methods of BigInteger class.
import java.math.BigInteger;
import java.util.Scanner;
public class Factorial {
public static BigInteger findFactorial(int number){
// declare variable
BigInteger fact = BigInteger.ONE;
for(int i=1; i<=number; i++) {
fact = fact.multiply(BigInteger.valueOf(i));
}
return fact;
}
public static void main(String[] args) {
// declare variables
int number = 0;
//create Scanner class object
// to take input
Scanner scan = new Scanner(System.in);
// take input from end-user
System.out.print("Enter integer number:");
number = scan.nextInt();
// display result
System.out.println("Factorial = "+
findFactorial(number));
// close Scanner class object
scan.close();
}
}
Output:-
Enter an integer number:: 50
Factorial = 30414093201713378043612608166064768844377641568960512000000000000
Factorial Program in Java using While loop
import java.util.Scanner;
public class Factorial {
public static long findFactorial(int number) {
// declare variable
long fact = 1;
int i = 1;
while(i<=number) {
fact = fact * i;
i++;
}
return fact;
}
public static void main(String[] args) {
// declare variables
int number = 0;
long result = 0;
//create Scanner class object
Scanner scan = new Scanner(System.in);
// take input from end-user
System.out.print("Enter integer number:");
number = scan.nextInt();
// Find factorial
result = findFactorial(number);
// display result
System.out.println("Factorial = "+ result);
// close Scanner class object
scan.close();
}
}
This program calls findFactorial()
method which is developed by using a while loop and increment operator. It calculates the factorial value as 5! = 1*2*3*4*5
Similar to the previous case, we can also use the decrement operator to calculate the factorial value. It will calculate the factorial value as 5! = 5*4*3*2*1
Java method to find factorial using while loop and decrement operator,
public static long findFactorial(int number) {
// declare variable
long fact = 1;
int i = number;
// loop to repeat the process
while(i>1) {
fact = fact * i;
i--;
}
return fact;
}
Now we will see the recursive method to find the factorial value.
Java Factorial Method
Java factorial method using recursion technique
Formula,
n! = n * (n-1)!
The base case for finding factorial
factorial(0) = 1;
Or,
factorial(1) = 1;
General case for finding factorial
factorial(n) = n * factorial(n-1);
Java code for factorial using recursion method and if-else statement,
// recursive Java method to
// find factorial of a number
// using if-else statement
public static long findFactorial(int number) {
if(number == 0) return 1;
else
return number*findFactorial(number-1);
}
Java code for factorial using recusion method and ternary operator,
This method can find factorial of a given number in a single line.
// 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);
}
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!