# Factorial of a number Program in Java

## Factorial of a number in Java :

Factorial of a number is the product of all numbers between 1 and the number itself.

• For 0, the factorial is 1
• For negative numbers, the factorial values are not defined ## Methods Discussed

• Method 1: Iterative
• Method 2: Recursive
• Method 3: BigIntegers
• Method 4: One line Ternary Operator

### Method 1

• Initialize variable fact = 1
• Run a loop between 1 and fact with iteration in (i)
• For each iteration in (i), multiple it with fact
• fact = fact * i
• Print the result
```//Java program to find factorial of a number
import java.util.Scanner;
public class LearnCoding
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = sc.nextInt();

if(num >= 0)
{
System.out.println(num + " Factorial: " + getFact(num));
}
else
System.out.println("Negative Number: No Factorial");
}

private static int getFact(int num) {

if(num == 1 || num == 0)
return 1;

return num * getFact(num-1);
}
}```

#### Output

```Enter a number : 5
5 factorial: 120```

## Method 2 (Recursive)

This method uses recursion in Java. For an input num –

• Call a recursive function getFact(num)
• Set base case as num == 1, return 1
• For other cases return num * getFact(num-1);

Below is the code for the same –

```//Java program to find factorial of a number
import java.util.Scanner;
public class LearnCoding
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = sc.nextInt();

if(num >= 0)
{
System.out.println(num + " Factorial: " + getFact(num));
}
else
System.out.println("Negative Number: No Factorial");
}

private static int getFact(int num) {

if(num == 1 || num == 0)
return 1;

return num * getFact(num-1);
}
}```

#### Output

```Enter a number : 6
6 Factorial: 720```

## Method 3 (BigInteger)

Factorial of a number can be very very large. We can use long instead of int but still factorial will become big enough to not to be stored in long integers as well.

```// Java program to find factorial of a number of large numbers
import java.math.BigInteger;
import java.util.Scanner;
public class LearnCoding2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num = 30;
BigInteger fact = BigInteger.ONE;

for(int i = 1; i <= num;i++)
{
// we are doing fact = fact * i
// big integer multiplication happens like this
fact = fact.multiply(BigInteger.valueOf(i));
}

System.out.println(num + " Factorial: \n" + fact);

}
}```

#### Output

```30 Factorial:
265252859812191058636308480000000```

## Method 4 (One line Ternary Operator)

We can use the ternary operator to find factorial in one single line.

Note this also uses recursion in Java

```import java.util.Scanner;
public class LearnCoding
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = sc.nextInt();

if(num >= 0)
{
System.out.println(num + " Factorial: " + getFact(num));
}
else
System.out.println("Negative Number: No Factorial");
}

private static int getFact(int num) {

// single line to find factorial
return (num == 1 || num == 0) ? 1 : num * getFact(num - 1);
}
}```

#### Output

```Enter a number : 5
5 Factorial: 120```

### One comment on “Factorial of a number Program in Java”

• Srikanth

code in python
num = int(input(“Enter a number “))
fact=1
if num <= 0:
print("negative factorial")
elif num == 1:
print("1 is the factorial")

else:
for i in range(1,num+1):
fact=fact*i
print(fact) 0