The program first imports the java.util.Scanner package to allow user input. It then declares a class called Factorial and a main method, which is the entry point of the program.
Within the main method, the program prompts the user to enter a number for which they want to calculate the factorial and stores the input in the variable num. It then initializes a variable fact to 1.
Next, the program runs a for loop from 1 to num, multiplying the value of fact by each number in the loop to calculate the factorial. For example, if num is 5, the loop will run from 1 to 5, multiplying fact by 1, then 2, then 3, and so on, until the loop ends at 5. Finally, the program prints out the calculated factorial value.
import java.util.Scanner; class Factorial { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Factorial Number : "); int num = input.nextInt(); int fact = 1; for(int i=1; i<=num; i++) { fact *= i; } System.out.println("Factorial: "+ fact); } }
Enter Factorial Number : 5 Factorial: 120
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions