Java program to find factorial of a number
In this post, you will learn how to find the factorial of a number using the Java programming language.
The factorial is normally used in different areas of mathematics, including algebra, mathematical analysis, and combinations and permutations. The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as-
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
Example of factorial
Suppose we want to get a factorial of 7, then the factorial will be-
7! = 7 X 6 X 5 X 4 X 3 X 2 X 1
5040
There are different ways to write a Java programme to find the factorial of a number. Here we have mentioned three ways:
Find factorial of a number using for loop in Java
In the given program, we have used the for loop to iterate through all numbers between 1 and the given number 7, and get the product of each number.
public class FindFactorial{
public static void main(String args[]){
int x,fact=1;
int num=7;
for(x=1;x<=num;x++){
fact=fact*x;
}
System.out.println("Factorial of "+num+" is: "+fact);
}
}
Output of the above code:
Factorial of 7 is: 5040
Find factorial of a number using while loop in Java
In the given program, we have used the while loop to iterate through all numbers between 1 and the given number 6, and get the product of each number.
public class FindFactorial{
public static void main(String args[]){
int num = 6, i = 1;
long fact = 1;
while(i <= num)
{
fact *= i;
i++;
}
System.out.printf("Factorial of %d = %d", num, fact);
}
}
Output of the above code:
Factorial of 6 = 720
Find factorial of a number using do while loop in Java
In the given Java program, we have used the do while loop to find the factorial of a number.
import java.util.Scanner;
public class FindFactorial
{
public static void main(String arg[])
{
long num,fact=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number: ");
num=sc.nextLong();
int i=1;
do
{
fact=fact*i;
i++;
}
while(i<=num);
System.out.println("Factorial of "+num+" is: "+fact);
}
}
Output of the above code:
Enter number: 7
Factorial of 7 is: 5040
Find factorial of a number using recursion in Java
Here is the Java program to find the factorial of a number using the recursion function. A recursion function is a function that is called by itself. If the number is equal to 0, it returns 1. The factorial of a number less than equal to 0 does not exist. If the number is greater than 1, the function calls recursively and finds the factorial of a number.
public class FindFactorial{
static int factorial(int num){
if(num == 0)
return 1;
else
return(num * factorial(num-1));
}
public static void main(String args[]){
int i,fact=1;
int number=4;
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output of the above code:
Factorial of 6 = 720
Find factorial of a number using single line solution in Java
Here, we have mentioned the simplest solution to find the factorial of a number in Java.
public class FindFactorial{
static int factorial(int n)
{
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
public static void main(String args[]){
int fact = 1;
int num = 8;
fact = factorial(num);
System.out.println("Factorial of "+num+" is: "+fact);
}
}
Output of the above code:
Factorial of 8 is: 40320
Related Articles
Java random number between 1 and 10Java program to find the greatest of three numbers
Priority queue in Java
Queue implementation in Java
Area of circle program in Java
Difference between vector and arraylist in Java
Count vowels in a string Java
Rotate array in Java
Largest number in array Java
Square root program in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year