In this tutorial we will learn writing program in java to find the factorial of a number using for loop, while loop, functions and recursions.
Before start writing the program Lets first know
What is Factorial of a number?
Factorial of a whole number ‘n’ is the product of that number with every whole number till 1.
Factorial of zero is equal to 1.
Basically Factorial of any number n is the product of all positive descending integers till 1. And we denotes Factorial using symbol !. Like if we have to tell factorial of 6 so we will it as 6!.
For example:
6! = 6 * 5 * 4 * 3 * 2 * 1
Here, We will pronounced 6! Is as “6 factorial” or factorial of 6.
Program 1: Calculate Factorial In Java using For loop
import java.util.*;
public class Main
{
public static void main(String[] args) {
long i,num,factorial=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to calculate factorial: ");
num = sc.nextInt();
if(num<0){
System.out.println("Factorial can't be calculated for negative number ");
}
else if(num==0){
System.out.println("Factorial of 0 is 1");
}else{
for(i=1;i<=num;i++)
factorial=factorial*i;
System.out.println("Factorial = "+factorial);
}
}
}
Output
Enter a number to calculate factorial:
5
Factorial = 120
Program 2: Calculate Factorial In Java using method and while loop
import java.util.*;
public class Main
{
static long fact(long num){
long i=1,fact=1;
while(i<=num){
fact*=i;
i++;
}
return fact;
}
public static void main(String[] args) {
long num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a whole number to find Factorial ");
num= sc.nextInt();
System.out.println("Factorial = "+fact(num));
}
}
Output
Enter a whole number to find Factorial
6
Factorial = 720
Program 3: Factorial Program In Java using recursion
import java.util.*;
public class Main
{
static long fact(long num){
if (num == 0)
return 1;
else
return(num * fact(num-1));
}
public static void main(String[] args) {
long num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a whole number to find Factorial ");
num= sc.nextInt();
System.out.println("Factorial = "+fact(num));
}
}
Output
Enter a whole number to find Factorial
5
Factorial = 120
Program 4 : Find Factorial using Java 8
import java.util.*;
import java.util.stream.*;
public class Main
{
public static void main(String[] args) {
long num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a whole number to find Factorial ");
num = sc.nextInt();
long factorial = LongStream.rangeClosed(1, num)
.reduce(1, (long x, long y) -> x * y);
System.out.println("Factorial = "+factorial);
}
}
Output
Enter a whole number to find Factorial
6
Factorial = 720
Conclusion
In this tutorial we have learnt how to calculate the factorial of a number in Java. We have seen various methods to calculate java including for loop, while loop, recursive function, and Java 8 streams.
Before writing program, in this tutorial we have explained the concept of factorial. The notation for factorial was introduced using the symbol ‘!’.
We have seen four different ways to write programs to calculate factorial:
- Using For Loop: In this program we have used a simple for loop to iterate from 1 to the given number. Each time multiplying each value along the way to calculate the factorial.
- Using Method and While Loop: Here we have created a method to write the factorial logic, and used a while loop was to perform the multiplication iteratively.
- Using Recursion: We have also seen writing factorial program using recursion. We have defined a function that called itself again and again.
- Using Java 8 Streams: And finally we have seen writing factorial program using java 8 stream. We have used Java 8 streams, where the
LongStream
class was used to generate a stream of values, and thereduce
operation was used to calculate the factorial.
Through these examples, I hope you have cleared how to write factorial program in java. The tutorial aimed to enhance the understanding of both fundamental and advanced concepts, providing a solid foundation for implementing factorial calculations in Java.
Happy coding!