ISC Practicals and Theory - Factorial
Updated 20 Oct 2026
The factorial of a non-negative whole number is the product of all whole numbers from 1 up to that number. This note finds it with a loop that multiplies the numbers one by one into a running product.
Definition
The factorial of a number $n$, written $n!$, is the product of all positive integers from 1 to $n$. By definition $0! = 1$.
$n! = 1 \times 2 \times 3 \times \dots \times n$
For example:
- $4! = 1 \times 2 \times 3 \times 4 = 24$
- $0! = 1$, because the product of no numbers is taken as 1.
Algorithm
- Start. In
main, create an objectobof classFactorial. - Call
input(): it accepts the number and stores it in the data membern. - Call
display():- If
nis less than 0 or greater than 20, print an error message and stop. Alongcannot hold $21!$, so 20 is the largest number allowed. - Otherwise call
fact()and print the result.
- If
- The method
long fact()works like this:- Set
f = 1. - Repeat for
ifrom 1 ton:f = f * i. - Return
f. Whennis 0 the loop does not run and 1 is returned, which is correct for $0!$.
- Set
- Stop.
Program
The constructor sets n to 0, input() reads the number, fact() multiplies the numbers 1 to n in a for loop, and display() checks the range and prints the answer.
import java.util.Scanner;
public class Factorial
{
int n;
Factorial()
{
n = 0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number (0 to 20): ");
n = sc.nextInt();
}
long fact()
{
long f = 1;
for (int i = 1; i <= n; i++)
f = f * i;
return f;
}
void display()
{
if (n < 0 || n > 20)
System.out.println("Invalid input. Enter a number from 0 to 20.");
else
System.out.println("Factorial of " + n + " = " + fact());
}
public static void main(String args[])
{
Factorial ob = new Factorial();
ob.input();
ob.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member that stores the number entered by the user |
sc | Scanner | Object used to read the number from the keyboard |
f | long | Running product; holds the factorial found so far and is returned |
i | int | Loop counter that takes the values 1 to n |
args | String[] | Command-line arguments of main (not used) |
ob | Factorial | Object of the class created in main to call the methods |
Sample input and output
Sample 1
Enter a number (0 to 20): 5
Factorial of 5 = 120Sample 2
Enter a number (0 to 20): 0
Factorial of 0 = 1Sample 3
Enter a number (0 to 20): 21
Invalid input. Enter a number from 0 to 20.Dry run
Input: n = 5. Start with f = 1.
| i | f = f * i |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 24 |
| 5 | 120 |
After i = 5 the condition i <= n fails for i = 6, the loop ends and fact() returns 120. The output is Factorial of 5 = 120, the same as the real run above.
Variations
- Strong number "A number is a strong number if the sum of the factorials of its digits equals the number. Write a program to check it, using a method to find the factorial."
- Sum of digit factorials "Accept a number and display the sum of the factorials of its digits."
- Combinations "Write a program to find nCr, using a method that returns the factorial of a number."