In this tutorial, we are going to learn java program to find the factorial of the number given as input from the user using for loop.

## Problem Statement

For any Input numbers given by the user, we have to calculate the factorial of that numbers.

**For example**

**Case1:** If the user input the 5.

then the output should be 120 ( 1**2**3**4**5 = 120).**Case2:** If the user input the 6.

then the output should be 720 (1**2**3**4**5*6 =720).

## What is the Factorial of a Number?

The factorial of a number is the multiplication of each and every number till the given number except 0.

**For example**

Factorial of number 4 is the multiplication of every number till number 4, i.e. 1**2**3*4 = 24

## Important Point

**• The factorial of 0 is 1• The factorial of any negative number is not defined.• The factorial only exits for whole numbers**

## Our logic to Calculate Factorial of number using for loop

• Our program will create three variables i, num, factorial of long types

• Our program will take an integer input from the user which should be a whole number.

• If the number input is negative then print ‘The factorial can’t be calculated’.

• Then use for loop to calculate the factorial.

• After that, we will store the value of the factorial in a variable and finally print the value of the variable.

## Algorithm to calculate Factorial of Number

Step 1: Start

Step 2: declare variable i, num, factorial in long data-type

Step 3: initialize variable ‘factorial’ with the value 1.

Step 4: Take an integer input from user to calculate the factorial

Step 4: if(number<0):

print ‘cannot be calculated.

Else if ( number == 0):

print 1

else: for (i=1; i<=num, i++)

factorial=factorial*i

Step 5: print factorial

Step 6: Stop

## Java Program to Calculate Factorial using For Loop

```
import java.util.*;
public class Main
{
public static void main(String[] args) {
long i,num,factorial=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to calculate factorial: ");
num = sc.nextInt();
if(num<0){
System.out.println("Factorial can't be calculated for negative number");
}
else if(num==0){
System.out.println("Factorial of 0 is 1");
}
else{
for(i=1;i<=num;i++)
factorial=factorial*i;
System.out.println("Factorial = "+factorial);
}
}
}
```

**Output** **1**

**Explanation**

The input number from the user to find the factorial is 5. The calculated value of the factorial is 120.

**Output 2**

The input number from the user to find the factorial is 8. The calculated value of the factorial is 40320.

### Breaking Down the Program

**Imports and Class Definition**

```
import java.util.*;
public class Main
```

: This line tells Java to bring in all classes from the`import java.util.*;`

`java.util`

package which includes the`Scanner`

class used for input.: This defines a class named`public class Main`

`Main`

. In Java, everything runs inside classes, and this is the main container for our code.

**Main Method**

`public static void main(String[] args)`

This is the entry point of any Java application that runs as a standalone program. The `main`

method is always called when the program starts.

**Variable Declaration and Initialization**

```
long i, num, factorial = 1;
Scanner sc = new Scanner(System.in);
```

**Variables**:`i`

,`num`

, and`factorial`

are declared.`factorial`

is initialized to 1 because the factorial calculation involves multiplication, and starting with 1 ensures the correct calculation.**Scanner**: A`Scanner`

object`sc`

is created to read input from the user. This allows the program to take numbers inputted by the user.

**Prompt for Input**

```
System.out.println("Enter a number to calculate factorial: ");
num = sc.nextInt();
```

The program prints a message asking the user to enter a number. Then, it uses `sc.nextInt()`

to read the integer input and stores it in `num`

.

**Conditionals and Loop for Calculation**

```
if (num < 0) {
System.out.println("Factorial can't be calculated for negative number");
} else if (num == 0) {
System.out.println("Factorial of 0 is 1");
} else {
for (i = 1; i <= num; i++)
factorial = factorial * i;
System.out.println("Factorial = " + factorial);
}
```

**Negative Check**: If`num`

is less than 0, it prints an error message because factorials of negative numbers are not defined.**Zero Check**: If`num`

is 0, it prints that the factorial of 0 is 1 (by definition).**Positive Numbers**: If`num`

is positive, it enters a loop from 1 up to`num`

. Inside the loop,`factorial`

is updated by multiplying it by each number`i`

(this is how factorials are calculated).- After the loop, it prints the calculated factorial.