factorial (n) = n x (n1) x (n2) ... 1
Java Program to find Factorial of a number
Carvia Tech  October 17, 2020  2 min read  238 views  Java Coding Challenges
Factorial of a number is calculated using below mathematical formula:
In Java, we can either use iterative approach or recursive approach to calculate the factorial. We will discuss both of them in this article.
Recursive approach
The factorial of a number can be expressed by this recursive function:
factorial (n) = n x factorial (n1)
till n becomes 1 i.e. till factorial of 0 (which is one).
public class Factorial {
public long factorial(long n) {
if (n == 1) (1)
return 1;
else {
return n * factorial(n  1); (2)
}
}
}
1  Base condition for recursive method. Factorial of Zero is One. 
2  We multiply number with factorial of (number  1) to get factorial of a number 
Iterative approach
Iterative approach is bit simple, where we multiply all numbers from 1 to n and return the result.
class Scratch {
public long factorial(long number) {
long result = 1L;
while (number != 0) {
result = result * number;
number;
}
return result;
}
}
Factorial using Java 8 Streams
Java 8 streams with reduction method can be used to calculate factorial of a number. This is a iterative approach, but instead of loops we are using streams.
import java.util.stream.LongStream;
class Scratch {
public static long factorialStreams(long n) {
return LongStream.rangeClosed(2, n)
.reduce(1, (long a, long b) > a * b);
}
}

LongStream.rangeClosed(2, n) method creates a Stream of longs from [2 to n].

Lambda function supplied for reduce
(a,b) → a * b
will multiply each pair ofa
andb
and return the result. The result then carries over toa
for the next round. The value of1
is used in the reduction method as the starting value for variablea
for the first iteration.
Handling very large numbers
Here we are using long
to hold the result of multiplication, which can quickly overflow due to maximum limit an long can hold. We can improve the above two programs to use BigInteger as discussed in the below article:
Java Coding Challenges:
 Reverse position of words in a string using recursion
 Check if the given string is palindrome
 Find two numbers of which the product is maximum in an array
 Check a number is Prime: Java Coding Problem
 Create anagram buckets from a given input array of words
 Anagrams string checker in Java
 Program to reverse a string using recursion in Java
Top articles in this category:
 SDET Java Coding Challenges
 Find two numbers of which the product is maximum in an array
 Check a number is Prime: Java Coding Problem
 Write a program to reverse the order of words in a string
 50 SDET Java Interview Questions & Answers
 How to reverse a number in Java
 Armstrong Number in Java
Find more on this topic:
SDET Interviews
SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured
Last updated 1 week ago
Recommended books for interview preparation:
Similar Posts
 Reverse position of words in a string using recursion
 REST Assured with plain/text response body
 Get distinct words from a given file in Java
 SDET Java Coding Challenges
 REST Assured vs Apache HttpClient and RestTemplate
 Java 11 HttpClient with Basic Authentication
 HTTP GET request with Java 11 HttpClient  Kotlin
 HTTP Head request using Java 11 HttpClient  Kotlin
 Using Java 11 HttpClient with Kotlin Coroutines
 Migrating Spring Boot tests from Junit 4 to Junit 5