Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Start. In main, create an object ob of class Factorial.
  2. Call input(): it accepts the number and stores it in the data member n.
  3. Call display():
    • If n is less than 0 or greater than 20, print an error message and stop. A long cannot hold $21!$, so 20 is the largest number allowed.
    • Otherwise call fact() and print the result.
  4. The method long fact() works like this:
    • Set f = 1.
    • Repeat for i from 1 to n: f = f * i.
    • Return f. When n is 0 the loop does not run and 1 is returned, which is correct for $0!$.
  5. 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

VariableData TypePurpose
nintData member that stores the number entered by the user
scScannerObject used to read the number from the keyboard
flongRunning product; holds the factorial found so far and is returned
iintLoop counter that takes the values 1 to n
argsString[]Command-line arguments of main (not used)
obFactorialObject 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 = 120

Sample 2

Enter a number (0 to 20): 0
Factorial of 0 = 1

Sample 3

Enter a number (0 to 20): 21
Invalid input. Enter a number from 0 to 20.

One value per line

Dry run

Input: n = 5. Start with f = 1.

if = f * i
11
22
36
424
5120

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

  1. 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."
  2. Sum of digit factorials "Accept a number and display the sum of the factorials of its digits."
  3. Combinations "Write a program to find nCr, using a method that returns the factorial of a number."