Factorial Program in Java: How to find factorial of a number?

Last updated on Jun 14,2021 8.4K Views
A technophile with a passion for unraveling the intricate tapestry of the... A technophile with a passion for unraveling the intricate tapestry of the tech world. I've spent over a decade exploring the fascinating world of...

Factorial Program in Java: How to find factorial of a number?

edureka.co

As a beginner, you will often come across a factorial program in Java interview. In Layman’s term, Factorial of a positive integer is the product of all descending integers. Factorial of a number(n) is denoted by n!. Also, factorial of 0 is 1 and it is not defined for negative integers. Here’s a simple representation to calculate factorial of a number-

             n! = n*(n-1)*(n-2)* . . . . . *1

There are multiple ways to find factorial in Java, which is listed below-


Let’s get started.

Factorial Program using For Loop

This is one of the easiest programs to find factorial of a number using ‘For Loop’. Let’s dive into an example and find a factorial of a given input.

public class FactorialProgram{  
 public static void main(String args[]){  
  int i,fact=1;    // defining fact=1 since least value is 1
  int number=5;    // given input to calculate factorial
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  System.out.println("Factorial of "+number+" = "+fact);    
 }  
}  

Output: Factorial of 5 = 120

Explanation: The number whose factorial is to be found is taken as input and stored in a variable ‘number’. Here, we have initialized fact=1 since least value is 1. Then, we’ve used for loop to loop through all the numbers between 1 and the input number(5), where the product of each number is stored in a variable ‘fact’.

Note: The logic of the factorial program remains the same, but the execution differs.

Now that you are clear with the logic, let’s try to implement the factorial program in Java in another way i.e using while loop.

Factorial program in Java using while loop

While loop in Java help your code to be executed repeatedly based on the condition. Let’s visit the code and implement the factorial program in Java using while loop.
Do let us know if you face any errors or doubts related to the program.

public class FactorialProgram  
{

    public static void main(String[] args) {

        int number = 5; // user-defined input to find factorial
        long fact = 1; // defining fact=1 since least value is 1
        int i = 1;
        while(i<=number)
        {
            fact = fact * i;
            i++;
        }
        System.out.println("Factorial of "+number+" = "+fact);
    }
}

Output: Factorial of 5 = 120

Explanation- In the above program, the value of i is incremented inside the body of the loop. As I have already mentioned above, the logic remains the same for factorial in java, just the execution differs.

Moving ahead, let’s implement factorial in Java using recursion.

Factorial program in Java using Recursion

Recursion is a function or a method which calls itself continuously. You can use recursive methods which call itself, thereby making the code short but a little complex to understand. Let’s understand more about recursion by visiting the below code.

public class FactorialProgram
{
	 static int factorial(int n){    
	  if (n == 0)    
	    return 1;    
	  else    
	    return(n * factorial(n-1));    
	 }    
	 public static void main(String args[]){  
	  int i,fact=1;  
	  int number=5; // user-defined input to find factorial    
	  fact = factorial(number);   
	  System.out.println("Factorial of "+number+" is = "+fact);    
	 }  
	}  

 

Output- Factorial of 5 is= 120

Explanation: In the above code, I have created a recursive method factorial which calls itself until the condition has met.

This brings us to the end of this article where we have learned how to implement factorial program in Java. Hope you are clear with all that has been shared with you in this tutorial. Make sure you practice as much as possible and revert your experience!

If you wish to learn

If you found this article on “factorial program in Java” relevant, check out the Edureka’s Java Course, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “factorial program in Java” and our team will be glad to answer.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework