The factorial of n numbers can be denoted as n!, it is the product of all number less than or equal to n
n! = 1 * 2 * 3* . . . . (n-2) * (n-1) * n
In this article, we will create the Factorial Program in Java using the below 4 ways
- Using For loop
- Using While loop
- Using Do While loop
- Using Recursion
Example 1: Factorial Program in Java using For loop
public class FactorialProgram { public static void main(String[] args) { int number = 6; long factorial = 1; for (int i = 1; i <= number; i++) { factorial = factorial * i; } System.out.println("Factorial of " + number + " is: " + factorial); } }
In the above code, we used a for loop to iterate through the numbers 1 to the given number [6] and during each iterations product is saved to the factorial variable.
Example 2: Factorial Program in Java using While loop
package com.javainterviewpoint; public class FactorialProgram { public static void main(String[] args) { int number = 6; long factorial = 1; int i=1; while (i <= number) { factorial = factorial * i; i++; } System.out.println("Factorial of " + number + " is: " + factorial); } }
The above code is almost the same, instead of a for loop we are using the while loop and the loop incrementation happens inside the body of the loop (i++)
Example 3: Factorial Program in Java using Do While loop
package com.javainterviewpoint; public class FactorialProgram { public static void main(String[] args) { int number = 6; long factorial = 1; int i = 1; do { factorial = factorial * i; i++; } while (i <= number); System.out.println("Factorial of " + number + " is: " + factorial); } }
The difference between while loop and do while loop is that, in while loop the condition is checked at the beginning of each iteration and in do while loop the condition is checked at end of each iteration
Example 4: Factorial Program in Java using Recursion
package com.javainterviewpoint; public class FactorialProgram { public static void main(String[] args) { int number = 6; long factorial = calculateFactorial(number); System.out.println("Factorial of " + number + " is: " + factorial); } public static long calculateFactorial(int number) { if (number == 1) return 1; else return number * calculateFactorial(number -1); } }
In the above code, we will be passing the number to the calculateFactorial() method, till the number is greater than 1 then the number is multiplied with calculateFactorial() recursively where number -1 is passed to it.
Example 5: Factorial Program in Java using Scanner
package com.javainterviewpoint; import java.util.Scanner; public class FactorialProgram { public static void main(String[] args) { int number = 6; long factorial = 1; Scanner scanner= new Scanner(System.in); System.out.println("Enter the Number : "); number = scanner.nextInt(); for (int i = 1; i <= number; i++) { factorial = factorial * i; } System.out.println("Factorial of " + number + " is: " + factorial); } }
Scanner is a class in java.util package, it can be used to read input from the keyboard. We will be getting the input the from the user for which the factorial needs to be calculated and factorial is calculated using for loop.
Output:
Enter the Number : 5 Factorial of 5 is: 120
Example 6: Factorial Program in Java using Command Line Arguments
package com.javainterviewpoint; public class FactorialProgram { public static void main(String[] args) { int number = 6; long factorial = 1; number = Integer.parseInt(args[0]); for (int i = 1; i <= number; i++) { factorial = factorial * i; } System.out.println("Factorial of " + number + " is: " + factorial); } }
Output :
Run the program passing the command line argument “java Factorial Program 6”
If you are using eclipse IDE then follow the below steps to pass command line argument
- Click on Run -> Run Configurations
- Click on Arguments tab
- In Program Arguments section , Enter your arguments.
- Click Apply
Leave a Reply