Java Program to Find the Factorial of a Number
Java Program to Find the Factorial of a Number
In this tutorial, we will learn how to find the factorial of a number in java. The factorial of a number is the product of all the integers from 1 to that number. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java.
Input: Enter the number: 5
Output: Factorial of the entered number is: 120
Program 1: Find the Factorial of a Number
In this program, we will learn how to find the factorial of a number using a while loop.
Algorithm
- Start
- Create an instance of the Scanner Class.
- Declare a variable.
- Ask the user to initialize the variable.
- Declare a loop variable and another variable to store the factorial of the number.
- Initialize both the variables to 1.
- Use a while loop to calculate the factorial.
- Run the loop till the loop variable is less than or equal to the number.
- Update the factorial in each iteration.
- Increment the loop variable in each iteration.
- Print the factorial of the number.
- Stop.
Below is the code example to print a factorial of a number in Java.
//Java Program to find the Factorial of a Number
import java.util.*;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
//Declare and Initialize the variable
System.out.println("Enter the number: ");
int num=sc.nextInt();
int i=1,fact=1;
while(i<=num)
{
fact=fact*i;
i++;
}
System.out.println("Factorial of the number: "+fact);
}
}Output:
Enter the number: 5
Factorial of the number: 120
Program 2: Java Program to Find the Factorial of a Number
In this program, we will learn how to find the factorial of a number using a for loop.
Algorithm
- Start
- Create an instance of the Scanner Class.
- Declare a variable.
- Ask the user to initialize the variable.
- Declare a variable to store the factorial of the number.
- Initialize the variable to 1.
- Use a for loop to calculate the factorial.
- Update the factorial variable by multiplying it with the loop variable in each iteration.
- Print the factorial of the number.
- Stop.
Below is the code example to print a factorial of a number in Java.
//Java Program to find the Factorial of a Number
import java.util.*;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
//Declare and Initialize the variable
System.out.println("Enter the number: ");
int num=sc.nextInt();
int fact=1;
for(int i=1;i<=num;i++)
{
fact=fact*i;
}
System.out.println("Factorial of the number: "+fact);
}
}Output:
Enter the number: 4
Factorial of the number: 24
Program 3: Java Program to Find the Factorial of a Number
In this program, we will find the factorial of a number using recursion with user-defined values. Here, we will ask the user to enter a value and then we will calculate the factorial by calling the function recursively.
Algorithm
- Start
- Declare a variable to store a number.
- Ask the user to initialize the number.
- Check whether it is possible to calculate the factorial or not.
- If the number is greater than and equal to 0, then call a recursive function to calculate the factorial of the entered number.
- If the number is lesser than 0, print the message that it is not possible to calculate the factorial.
- If the entered number is 0 or 1, then return 1.
- If the entered number is other than 0 or 1, then calculate the factorial by recursively calling the same method.
- Return the result.
- Print the factorial of the entered number.
- Stop
Below is the code example to print a factorial of a number in Java.
/*Java Program to find factorial of a number using Recursive Function*/
import java.util.Scanner;
public class Main
{
//Driver Code
public static void main(String[] args)
{
//Take input from the user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number :");
int num = sc.nextInt(); //Input the number
if(num>=0)
{
//Call a recursive function to find the factorial
int factorial=findFactorial(num);
System.out.println("The factorial of the entered number is :"+factorial);
}
else
{
System.out.println("Factorial not possible.");
System.out.println("Please enter valid input.");
}
}
//Recursive Function to Find the Factorial of a Number
public static int findFactorial(int num)
{
if(num==0)
return 1;
else if(num==1)
return 1;
else
return num*findFactorial(num-1);
}
}Output:
Enter the number: 8
The factorial of the entered number is:40320










