Signup/Sign In

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

  1. Start

  2. Create an instance of the Scanner Class.

  3. Declare a variable.

  4. Ask the user to initialize the variable.

  5. Declare a loop variable and another variable to store the factorial of the number.

  6. Initialize both the variables to 1.

  7. Use a while loop to calculate the factorial.

  8. Run the loop till the loop variable is less than or equal to the number.

  9. Update the factorial in each iteration.

  10. Increment the loop variable in each iteration.

  11. Print the factorial of the number.

  12. 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);  
     }   
}


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

  1. Start

  2. Create an instance of the Scanner Class.

  3. Declare a variable.

  4. Ask the user to initialize the variable.

  5. Declare a variable to store the factorial of the number.

  6. Initialize the variable to 1.

  7. Use a for loop to calculate the factorial.

  8. Update the factorial variable by multiplying it with the loop variable in each iteration.

  9. Print the factorial of the number.

  10. 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); 
     }  
}


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

  1. Start

  2. Declare a variable to store a number.

  3. Ask the user to initialize the number.

  4. Check whether it is possible to calculate the factorial or not.

  5. If the number is greater than and equal to 0, then call a recursive function to calculate the factorial of the entered number.

  6. If the number is lesser than 0, print the message that it is not possible to calculate the factorial.

  7. If the entered number is 0 or 1, then return 1.

  8. If the entered number is other than 0 or 1, then calculate the factorial by recursively calling the same method.

  9. Return the result.

  10. Print the factorial of the entered number.

  11. 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);        
    }
}


Enter the number: 8
The factorial of the entered number is:40320



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.