Java Tutorial
This java program is used to calculate the factorial value for the given number as user input using for loop.
import java.util.Scanner; public class FactorialNumber { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Factorial Value"); System.out.println("-----------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); int factorial=1; for (int i=1; i<=number; i++){ factorial = factorial * i; } System.out.println("Factorial of Number '"+number+"'': "+factorial);; } }Output:
D:\Java_Programs>javac FactorialNumber.java D:\Java_Programs>java FactorialNumber Factorial Value ----------------------- Enter Number: 5 Factorial of Number '5'': 120
This java program is used to calculate the factorial value for the given number as user input using while loop.
import java.util.Scanner; public class FactorialNumber { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Factorial Value"); System.out.println("-----------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); int factorial=1; int i=1; while (i<=number){ factorial = factorial * i; i++; } System.out.println("Factorial of Number '"+number+"'': "+factorial);; } }Output:
D:\Java_Programs>javac FactorialNumber.java D:\Java_Programs>java FactorialNumber Factorial Value ----------------------- Enter Number: 6 Factorial of Number '6'': 720
This java program is used to calculate the factorial value for the given number as user input using do while loop.
import java.util.Scanner; public class FactorialNumber { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Factorial Value"); System.out.println("-----------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); int factorial=1; int i=1; do { factorial = factorial * i; i++; } while (i<=number); System.out.println("Factorial of Number '"+number+"'': "+factorial);; } }Output:
D:\Java_Programs>javac FactorialNumber.java D:\Java_Programs>java FactorialNumber Factorial Value ----------------------- Enter Number: 4 Factorial of Number '4'': 24
This java program is used to calculate the factorial value for the given number as user input using recursion.
import java.util.Scanner; public class FactorialNumber { public static int factorial(int number){ if (number == 1) { return 1; } return number * factorial(number -1); } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Factorial Value"); System.out.println("-----------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); int factVal = 1; factVal = factorial(number); System.out.println("Factorial of Number '"+number+"'': "+factVal);; } }Output:
D:\Java_Programs>javac FactorialNumber.java D:\Java_Programs>java FactorialNumber Factorial Value ----------------------- Enter Number: 6 Factorial of Number '6'': 720
This java program is used to calculate the factorial value for the given number as user input using recursion and ternary or conditional operator.
import java.util.Scanner; public class FactorialNumber { public static int factorial(int number){ return (number < 1)? number * factorial(number -1): 1; } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Factorial Value"); System.out.println("-----------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); int factVal = 1; factVal = factorial(number); System.out.println("Factorial of Number '"+number+"'': "+factVal);; } }Output:
D:\Java_Programs>javac FactorialNumber.java D:\Java_Programs>java FactorialNumber Factorial Value ----------------------- Enter Number: 5 Factorial of Number '5'': 120
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page