The WP Debugging plugin must have a wp-config.php file that is writable by the filesystem.

Java Program to Find the Factorial of a Number

Java Program to Find the Factorial of a Number

In this article, we will learn how to find the factorial of a number in java. The factorial of a number is the product of all integers from 1 to that number. Before moving forward make sure you are familiar with loops in java.

Example:
Enter the number: 5

Output:
A factorial of 5 is 120

Method 1: Find the factorial of a number in java.

In this method, we will learn how to find the factorial of a number in java using a while loop.

Algorithm to find the factorial of a given number using a while loop

  • Create an instance of a Scanner class.
  • Declare and initialize the new variable.
  • Declare a new variable for the loop and store the factorial of a number.
  • Initialize both variables with 1.
  • Traverse using a while loop.
  • Run the loop till the given number.
  • Update the factorial in each iteration of the number.
  • Increment the loop variable.
  • Print the factorial of a number.

Code Implementation of java program to find factorial of a number using while loop:


import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        
        Scanner sc=new Scanner(System.in);
       
        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

Method 2: Java program to find the factorial of a number

In this method, we will use a for loop to find the factorial number.

Algorithms to find the factorial of a given number using for loop.

  • Create an instance of a Scanner class.
  • Declare and initialize the new variable.
  • Declare a new variable for the loop and store the factorial of a number.
  • Initialize both variables with 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 given number.

Code Implementation java program to find factorial of a number using For loop:

import java.util.*;
public class Main
{
     public static void main(String []args)
     {       
        Scanner sc=new Scanner(System.in);       
        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); 
     }  
}

Method 3: Java program to find the factorial of a given number recursively.

In this method, we will find the factorial of a given number recursively, In this program, we will take input from the user and we calculate the factorial of a number recursively.

Algorithm to find the factorial of a given number recursively

  • Declare a variable to store a number.
  • Ask the user to enter the input 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 and print the factorial of a given number

Code Implementation of java program to find factorial of a number:

import java.util.Scanner;
public class Main
{
  
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number :");
        int num = sc.nextInt();   
        if(num>=0) 
        {
          
           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.");
        } 
    }
   
    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: 6
The factorial of the entered number is:720

Conclusion

In the above article, we have discussed different approaches to finding the factorial of a given number in Java. We hope this article helps you to enhance your knowledge and gives you a grasp to solve more similar problems. Also, you can visit PrepBytes for practicing more similar questions. Many serviced based companies like Wipro, TCS, Capgemini, Infosys, and Accenture have asked logic-building questions likes finding the factorial of a number in their process of hiring.

Other Java Programs
Java program to add two numbers
Java program to search an element in a linked list
Java program to convert arraylist to linkedlist
Java program to reverse a linked list

Leave a Reply

Your email address will not be published. Required fields are marked *