In this article, we will learn to write factorial code in Java. Java is one of the most popular programming languages worldwide, so we will solve some of the most common problems using the Java programming language.
The factorial problem is a well-known beginner problem that is often asked during technical or coding interviews. In mathematics, factorials are used in many places such as algebra, permutation, combinatorics, power series, probability, and more. Let us learn to write factorial code in Java using loops and recursion methods but first let us know what “factorial” actually is.
What is factorial?
- Factorial is the repeated product of a number with each number below it until it reaches the last number, i.e. 1. The factorial of a number N is the product of all positive numbers below that number until it reaches 1. It is denoted by N! For example,
Example of factorial |
6! = 6x5x4x3x2x1 = 7205! = 5x4x3x2x1 = 120 |
- In the above example, we can see that we find the factorial by repeatedly multiplying a number by all the numbers below it until the number reaches 1. The final multiplication answer. “6!” is pronounced as “6 factorial.”
There are several ways to write factorial code in Java. Let’s find out in this article.
How to write factorial code in java.
We can easily write factorial code in Java. Let us understand how to solve the given problem.
- Understand the logic of the problem: In the given situation, we know the formula to calculate the factorial of numbers. We will find our solution using the formulas.
- Use a For or While loop: You can use a For loop or a While loop to perform repeated multiplication and find the factorial of a given number.
- We can also use recursion to find the factorial using Java.
Factorial is the repeated product of numbers in decreasing order. We will take this signal and start our solution. Let’s consider these two methods in more detail.
Problem description for factorial program in Java
- Let us take an example of a problem statement that you might get while facing a factorial problem.
- With the above problem statement, you will find the constraints, time complexity and parameters. Here, we are considering both the methods to calculate the factorial, so, we are not considering the additional constraints.
Factorial code in Java using for-loop
Let us use for loop to compute the factorial of a given number in Java.
Let us use for loop to compute the factorial of a given number in Java. |
Factorial code in java using for loop public class factorial { public static zero main (string [] args) { int number = 6; / / given number is 6, we have to find the factorial. long factorial = 1; for (int i = 1; i < = number; i + +) { factorial * = i; } System. out. println (Factorial of a given number + Number + = + Factorial) } } |
Production
Factorial of the given number 5 = 120
explanation
- First, we will take a number inside the main function for which we want to compute the factorial.
- Now, run the for loop up to the given number.
- Continue multiplying the factorial with the index of the for loop until it is equal to or greater than the number.
- Return Factorial
The time complexity of the given solution is O (N), which becomes very long when the number is large and hence it is not considered to be the optimal solution for the given factorial program in Java.
Factorial code using while loop in Java.
Let us now use the while loop to find the factorial of a number in Java below.
Factorial code using While Loop in Java. |
Import java.util.scanner; Public Class FactorialWhileLoop { public static zero main (string [] args) { Scanner Scanner = New Scanner (System. in) System. out. print (Enter a number:)) int number = scanner. nextInt () long factorial = 1; / / using long to handle large factorial int i = 1; while (i < = number) { factorial * = i; I’m going to + +; } System. out. println (factorial of + number + = + factorial) } } |
Production
Enter a number:. 6 Factorial of 6 = 720. |
explanation
- Import the scanner class, we will use it to scan the input given by our users.
- First of all, declare a new variable and start.
- Now declare a new variable for the loop and store a number of factorials in it.
- Start both the variables with 1.
- Now, for traversal, we will use while loop up to the given number.
- We will keep updating the factorial in every iteration
- Increase loop variable i, ei
- Finally, print the factorial of a number for the users.
Factorial code in Java using recursion
Let us now use another method that is recursion to find the factorial of a given number using Java.
Factorial code in Java using recursion |
Import java.util.scanner; public class factorial recursion { public static zero main (string [] args) { Scanner Scanner = New Scanner (System. in) System. out. print (Enter a number:)) int number = scanner. nextInt () Long factorial = factorial (number) System. out. println (factorial of + number + = + factorial) } public static long factorial (int n) { if (n = 0) { return 1; / / the base case for the given function } Other { return n * factorial (n-1) / / Recursive calling } } } |
Production
Enter a number:. 6 Factorial of 6 = 720. |
explanation
- We will first import the scanner class, which can read the input given by the user.
- Declare a variable to store the factorial of the number.
- Ask the user to enter the number.
- Create a different function, i.e. factorial ()
- Take the number as a parameter.
- Create a base condition, i.e. when n value becomes 0, we will return 1.
- Keep calling the function recursively.
Factorial code in Java FAQ’s
Question 1: How do I find the factorial of a number using a loop?
Answer: Follow the steps below to find out the factorial of the number given below.
Start a variable with n, which is the number of which we have to factorialize. This is a fixed value, suppose n is 6. Now, another variable, the factorial, initializes the factorial to 1. During each iteration, store the previous answer and keep increasing the number.
Question 2: Which method is better for finding the factorial: recursion or iteration?
Answer: You can use the recursion method to find the factorial of a number, but it takes a lot of extra space and is also expensive. So, an iterative method is better than recursion to find the factorial. You can find the factorial in constant space using recursion which makes it more cost effective.
Question 3: What is the complexity of factorial using recursion?
Answer: The time complexity of the factorial using iteration is O (N), where N = is the given number.
YOU MAY BE INTERESTED IN
Table to check whether a business object has any GOS attachment or not