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 using a recursive method, that is, a method that calls itself on a smaller number until it reaches a number whose factorial is known.
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! = n \times (n-1) \times (n-2) \times \dots \times 2 \times 1$
For example:
- $4! = 4 \times 3 \times 2 \times 1 = 24$
- $0! = 1$
The same definition can be written in terms of itself: $n! = n \times (n-1)!$ for $n \ge 1$, with $0! = 1$. This is why recursion suits it.
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(n)and print the result.
- If
- The recursive method
long fact(int x)works like this:- Base case: if
xis 0 or 1, return 1. - Recursive case: return
x * fact(x - 1). - Each call waits for the smaller call to return, then multiplies.
- Base case: if
- Stop.
Program
The constructor sets n to 0, input() reads the number, fact(int x) calls itself to find the factorial, 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(int x)
{
if (x <= 1)
return 1;
return x * fact(x - 1);
}
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(n));
}
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 |
x | int | Parameter of fact; the number whose factorial is being found in the current call |
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): -3
Invalid input. Enter a number from 0 to 20.Dry run
Input: n = 5. The calls go down until the base case, then the values come back up.
| Call | x | Condition x <= 1 | Returns |
|---|---|---|---|
fact(5) | 5 | false | 5 * fact(4) (waits) |
fact(4) | 4 | false | 4 * fact(3) (waits) |
fact(3) | 3 | false | 3 * fact(2) (waits) |
fact(2) | 2 | false | 2 * fact(1) (waits) |
fact(1) | 1 | true | 1 |
Coming back: fact(2) returns $2 \times 1 = 2$, fact(3) returns $3 \times 2 = 6$, fact(4) returns $4 \times 6 = 24$ and fact(5) returns $5 \times 24 = 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."