Factorial in Java

This Java code defines a class Factorial that contains a main method. The main method prompts the user to enter a number and reads the input using a Scanner object. The code then calculates the factorial of the entered number using a for loop.

Inside the loop, the code initializes a variable f to 1 and multiplies it by the loop variable i for each iteration of the loop. The loop iterates from 1 to the entered number n.

Finally, the code prints the calculated factorial value of the entered number using the System.out.println method.

Source Code

import java.util.Scanner;
public class Factorial {
    public static void main(String args[])
        //1.Write a program to find the factorial of given number.
        Scanner in =new Scanner(System.in);
        System.out.println("Enter The Number : ");
        int n=in.nextInt();//5
        int f=1;
        for(int i=1;i<=n;i++)
        System.out.println("Factorial of "+n+" is "+f);


Enter The Number :
Factorial of 5 is 120
To download raw file Click Here

Basic Programs