factorial (n) = n x (n1) x (n2) ... 1
Calculate factorial of a number in Java using recursion
Carvia Tech  December 03, 2019  2 min read  66 views  Java Coding Challenges
Factorial of a number is calculated using below formula:
the factorial of a number is calculated by formula number * (number 1)
till zero and since the value of factorial zero is 1, it acts as a base case in the recursive version of the factorial method.
Recursive approach
public class Factorial {
public long factorial(long n) {
if (n == 1)
return 1;
else {
return n * factorial(n  1);
}
}
}
Iterative approach
class Scratch {
public long fact(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.
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:
 Check if the given number is Armstrong Number in Java
 Java program to check if two strings are anagrams
 Create anagram buckets from a given input array of words
 Find two numbers of which the product is maximum in an array
 Check if the given string is palindrome
 Check a number is Prime: Java Coding Problem
 How will you check if a given sentence is a pangram
Top articles in this category:
 Top 50 SDET Java Programming Interview Questions & Answers
 SDET: Rest Assured Interview Questions
 SDET: JUnit interview questions for automation engineer
 Check if the given number is Armstrong Number in Java
 Write a program to reverse a string using recursion in Java
 Calculate Fibonacci Series in Java
 Commonly used Http methods in RESTful services
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
 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
 Parameterized Tests using JUnit 5
 Creating custom Tag in Junit5 based tests
 Writing a simple Junit 5 test
 SDET: Rest Assured Interview Questions
Enter your email address to subscribe to this blog and receive notifications of new posts by email.