Java Program to Find Factorial of a Number
What is a Factorial Number ?
The factorial of a non-negative integer is the product of all positive integers less than or equal to that integer. For example, the factorial of 4 (written as 4!) is 4 x 3 x 2 x 1 = 24. Factorials are commonly used in combinatorics and probability.
Steps to Find the Factorial of a Number :
- Initialize a variable result to 1.
- Use a loop that starts from 1 and ends at n, incrementing by 1 each time.
- Multiply result by the current loop variable at each iteration.
- After the loop, return the value of result as the final answer.
Code example for the above algorithm :
public static int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }
Code example for recursion method :
public static int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } }
Example : Using a for loop
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = scanner.nextInt(); int result = 1; for (int i = 1; i <= n; i++) { result *= i; } System.out.println("The factorial of " + n + " is " + result); } }
Output :
Enter a positive integer: 5 The factorial of 5 is 120
Explanation :
In this example we determine the factorial of a user-input positive integer. It uses a for loop to iterate from 1 to the specified number, multiplying the loop variable’s current value by a result variable at each iteration. The given number’s factorial is the final value of the result variable. The program reads user input using the Scanner class and prints the outcome to the console.
Example : Using recursion
import java.util.*; public class Main { public static int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = scanner.nextInt(); int result = factorial(n); System.out.println("The factorial of " + n + " is " + result); } }
Output :
Enter a positive integer: 6 The factorial of 6 is 720
Explanation :
In this example, the factorial function is implemented in the code using recursion in Java. The factorial method determines the factorial of a given number n by returning 1 if n is zero and n * factorial(n-1) otherwise. Until n equals 0, the method calls itself with n-1 as the argument. The outcome of each call is then multiplied to create the overall outcome. The factorial method is called in the main method with a value of 5, and the result is printed.
Example : Using while loop
class Main { public static void main(String[] args) { int num = 5; System.out.println("The factorial of " + num + " is " + factorial(num)); } public static int factorial(int n) { int result = 1; while (n > 0) { result *= n; n--; } return result; } }
Output :
The factorial of 5 is 120
Explanation :
In this example the factorial function is implemented in Java using a while loop in the code. The while loop is used to multiply a variable result by n and decrement n until n is zero in the factorial method to calculate the factorial of a given number n. The factorial method’s output is the final value of the result. The factorial method is called in the main method with a value of 5, and the result is printed.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment