Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Factorial of a Number in Java refers to calculating the product of all positive integers up to a given number using Java programming. This concept is commonly implemented using loops or recursion, making it a fundamental exercise in control structures, methods, and mathematical logic in Java. Understanding factorial logic strengthens core algorithmic thinking.
What is a factorial program in Java?
A factorial program in Java is a Java-based implementation used to calculate the factorial of a number, which is the product of all positive integers less than or equal to that number. The program typically uses loops or recursion to perform the calculation. It helps beginners understand iteration, recursion, and method logic in Java.
Factorial of a number(non-negative) is the multiplication of all smaller integers than the number, including the number itself. We can write factorial of a number as,
Factorial Methods in Java
Factorial methods in Java are different approaches to calculate the factorial of a number, such as using iteration, recursion, loops, or BigInteger for large values.
Factorial Program using iteration in Java
Factorial Program using recursion in Java
Factorial Program using Loop in Java
Find the Factorial of a number using BigInteger
Factorial Java Program Using Ternary Operator
1.) Factorial Program using iteration in Java
The iteration method is discussed below:
Algorithm:
The steps for calculating factorial using iteration in java are as follows -
Initialise the result to 1
long Result = 1;
Run a loop in the range [1, number]
for(int i = 1 ; i <= Number ; i++) {}
In each iteration update the result.
Result = Result * i;
Finally, print the result
System.out.printf("The Factorial of %d is: %d ",Number,Result);
Code:
Java
Java
public class Factorial { public static void main(String[] args) { int Number = 6; // Given Number long Result = 1; for(int i = 1 ; i <= Number ; i++) { Result = Result * i; }
System.out.printf("The Factorial of %d is: %d ",Number,Result); } }
You can also try this code with Online Java Compiler
Store the result in a variable after the end of the recursion.
long Result = fact(Number);
Print the result.
System.out.printf("The factorial of Number %d is: %d",Number,Result);
Code:
Java
Java
public class Factorial { public static long fact(int Number) { if (Number == 0) return 1; else return Number * fact(Number - 1); }
public static void main(String[] args) { int Number = 6; long Result = fact(Number); System.out.printf("The factorial of Number %d is: %d", Number, Result); } }
You can also try this code with Online Java Compiler
The factorial of a number nnn is the product of all positive integers less than or equal to nnn. It is denoted as n!n!n!. This can be computed using a loop by iterating from 1 to nnn and multiplying the result at each step.
Here's an example of a factorial program using a loop in Java:
Java
Java
public class FactorialUsingLoop { public static void main(String[] args) { int number = 5; // Example number long factorial = 1;
for (int i = 1; i <= number; i++) { factorial *= i; }
System.out.println("Factorial of " + number + " is: " + factorial); } }
You can also try this code with Online Java Compiler
In this code, we initialize factorial to 1. We use a for loop to iterate from 1 to the given number. During each iteration, we multiply factorial by the current loop variable. Finally, we print the result.
4.) Find Factorial of a number using BigInteger
For very large numbers, the factorial result can exceed the range of primitive data types like int or long. In such cases, we use BigInteger from the java.math package, which can handle arbitrarily large integers.
Here's an example of finding the factorial of a number using BigInteger:
Java
Java
import java.math.BigInteger;
public class FactorialUsingBigInteger { public static void main(String[] args) { int number = 100; // Example large number BigInteger factorial = BigInteger.ONE;
for (int i = 1; i <= number; i++) { factorial = factorial.multiply(BigInteger.valueOf(i)); }
System.out.println("Factorial of " + number + " is: " + factorial); } }
You can also try this code with Online Java Compiler
Factorial of 100 is: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
In this code, we initialize factorial to BigInteger.ONE. We use a for loop to iterate from 1 to the given number. During each iteration, we multiply factorial by the current loop variable i converted to BigInteger using BigInteger.valueOf(i). Finally, we print the result.
5.) Factorial Java Program Using Ternary Operator
This method uses recursion combined with the ternary operator (? :) to compute the factorial in a compact form. The ternary operator simplifies conditional logic by handling the base and recursive cases in one line.
Code Example:
Java
Java
public class FactorialTernary { static int factorial(int n) { return (n == 0 || n == 1) ? 1 : n * factorial(n - 1); }
public static void main(String[] args) { int number = 5; int result = factorial(number); System.out.println("Factorial of " + number + " is: " + result); } }
You can also try this code with Online Java Compiler
This program demonstrates a concise way to calculate the factorial of a number using recursion and the ternary operator in Java.
Frequently Asked Questions
How to use factorial in Java?
To use factorial in Java, define a method using loops or recursion to multiply integers up to a given number and return the result.
How do you solve the factorial of 4?
4! = 4 * 3 * 2 * 1 = 24
Can we find a factorial of a negative number?
No factorial of a negative number is not possible. let’s understand why We can write n! = (n + 1)! / (n + 1). Like , 5! = 6! / 6 , 4! = 5! / 5. But for negative numbers like -1. -1! = 0! / 0 It is undefined. This applies to other negative numbers as well.
What is the time complexity of the factorial program?
For both the iteration and recursion solution, the time complexity of the factorial program is O(n), where n = number.
What is the space complexity of the factorial program?
For the iteration solution, the space complexity is O(1), and for the recursion solution, the space complexity is O(n) due to the call stack.(n = Number). For factorial n, a stack of size n will be allocated to store the functions' state.
What is an example of a factorial Program?
A factorial program calculates the product of all positive integers up to a given number nnn. In Java, using a loop or BigInteger for large numbers ensures accurate computation. Example: computing 5! = 5 * 4 * 3 * 2 * 1 = 120.
Conclusion
In this article, we have extensively discussed the factorial program and its implementations in Java.
In this blog, we started with the basic definition of factorial. Then we discussed the two possible methods, i.e., iterative and recursive approaches regarding the factorial program their implementations with the proper program flow diagram.