🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

WAP to find the factorial of a number in Java  


What are Factorial Numbers:
  • A factorial number is the product of all positive integers from 1 to that number.
  • It is written as n! (n factorial).
  • Examples:
    • 5! = 5 x 4 x 3 x 2 x 1 = 120
    • 3! = 3 x 2 x 1 = 6
    • 1! = 1
Logical Steps:
  • To find the factorial of a number, follow these steps:
    1. Take a number.
    2. Use a variable (fact) initialized to 1 to store the result.
    3. Use a Loop to Calculate Factorial:
      • Use a for loop starting from 1 to the given number (n).
      • Multiply the fact variable by the loop counter in each iteration.
    4. Print the value of fact after the loop completes.
Program:
  • Below is the simple program:
    public class FactorialProgram
    {
        public static void main(String[] args)
        {
            int no = 5;
    
            if (no < 0)
            {
                System.out.println("Factorial is not defined for negative numbers.");
            }
            else
            {
                long fact = 1;  // Variable to store the factorial result
    
                // Calculate factorial using a loop
                for (int i = 1; i <= no; i++)
                {
                    fact = fact * i;
                }
    
                // Display the result
                System.out.println("Factorial of " + no + " is: " + fact);
            }
        }
    }
    Output:
    Factorial of 5 is: 120
  • Below is the program by taking user input:
    import java.util.Scanner;
    
    public class FactorialProgram
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
    
            // Prompt the user to enter a number
            System.out.print("Enter a number to find its factorial: ");
            int no = scanner.nextInt();
    
            if (no < 0)
            {
                System.out.println("Factorial is not defined for negative numbers.");
            }
            else
            {
                long fact = 1;  // Variable to store the factorial result
    
                // Calculate factorial using a loop
                for (int i = 1; i <= no; i++)
                {
                    fact = fact * i;
                }
    
                // Display the result
                System.out.println("Factorial of " + no + " is: " + fact);
            }
    
            // Close the scanner
            scanner.close();
        }
    }
    Output:
    Enter a number to find its factorial: 6
    Factorial of 6 is: 720
Program Explanation:
  • Import Scanner Class:
    • The Scanner class is imported to take user input.
  • Declare a Variable:
    • An integer variable (no) is declared to store the user’s input.
  • Take Input:
    • The program prompts the user to enter a number to find the factorial number.
    • scanner.nextInt() is used to read and store the input in the no variable.
  • Check for Valid Input:
    • If n is less than 0, the program displays an error message (factorial is not defined for negative numbers).
  • Use a for Loop to Calculate Factorial:
    • The loop iterates from 1 to no. In each iteration, the fact variable is multiplied by the loop counter (i).
    • The result is stored back in fact.
  • Display the Result:
    • After the loop, the value of fact is printed as the factorial of the given number.
  • Close Scanner:
    • The scanner.close() method is used to close the input stream and release resources.