In this post we will learn about Java program to find factorial of a number.
Before Witting java program let’s understand what is Factorial number?
What is Factorial Number?
In mathematics language, we can say that the factorial of a non-negative integer n, denoted by n! is the product or multiplication of all positive integers less than or equal to n.
For example, 5 ! = 5 × 4 × 3 × 2 × 1 = 120.
The factorial of a negative number doesn’t exist. And the factorial of 0 is 1
We will solve this problem statement using below 2 approaches:
- Using for loop
- Using recursion
Example 1:
Using for loop
Below source code of the Java Program reads number from the user and finds factorial value using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * This Java program is all about How to find factorial of a input number using for loop */ public class ClientTest1 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Take a number from keyboard using Scanner System.out.println("Enter a Number:"); int number = scanner.nextInt(); int fact = findFactorial(number); System.out.println("Factorial of input " + number + " is " + fact); } catch (Exception e) { System.out.println("Please Enter valid number.."); e.printStackTrace(); } } //find factorial of a number using for loop private static int findFactorial(int number) { //Validation for negative number if(number<0){ throw new RuntimeException("The factorial of a negative number doesn’t exist.try With positive number"); } int factorial = 1; for (int i = number; i >= 1; i--) { factorial = factorial * i; } return factorial; } } |
Example 2:
Using recursion
Below source code of the Java Program reads number from the user and finds factorial value With using Recursion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * This Java program is all about How to find factorial of a input umber using recursion */ public class ClientTest2 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Take a number from keyboard using Scanner System.out.println("Enter a Number:"); int number = scanner.nextInt(); int fact = findFactorial(number); System.out.println("Factorial of input " + number + " is " + fact); } catch (Exception e) { System.out.println("Please Enter valid number.."); e.printStackTrace(); } } //find factorial of a number using recursion private static int findFactorial(int number) { //Validation for negative number if(number<0){ throw new RuntimeException("The factorial of a negative number doesn’t exist.try With positive number"); } //base case condition if (number == 1 || number == 0) { return 1; } else { return number * findFactorial(number - 1); } } } |
I have tested both programs for few inputs as below. You may check for other input as well
1 2 3 4 5 6 7 8 9 10 11 |
Enter a Number: 4 Factorial of input 4 is 24 Enter a Number: 5 Factorial of input 5 is 12 Enter a Number: 10 Factorial of input 10 is 3628800 |
Watch it on YouTube:
That’s all about this topic Java program to find factorial of a number
You May Also Like:
How to swap two numbers with or without temporary variable in java
Java Program to Swap two numbers using Bitwise XOR Operator?
How to reverse a number in Java
How to check Armstrong number java program
Java Program to Calculate the Power of a Number
Check whether a number is prime or not
Java Program to displaying prime numbers
Fibonacci series using iterative and recursive approach java program
Find largest and second largest number in the given Array
Java program to Remove Duplicate Elements From an Array
Find common elements between two Arrays
Find largest and smallest number in the given Array
Java Program to find duplicate elements in an Array
Count number of words in a string in java
How would you check if a number is even or odd using bit wise operator in Java?
How can you check if the given number is power of 2?
How to check if String is number in java ?
If you have any feedback or suggestion please feel free to drop in blow comment box.