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

ISC Practicals and Theory - Twin Prime

Updated 20 Oct 2026

Twin primes are two prime numbers that differ by 2, such as 11 and 13. This program reads a range m to n and displays every twin prime pair inside it. It is written as a class TwinPrime with data members and separate methods to read the range, test a number and display the pairs. The main method only creates an object and calls the methods.

Definition

A prime number has exactly two factors: $1$ and the number itself. Two primes $p$ and $q$ form a twin prime pair if $q - p = 2$.

  • $(11, 13)$ is a twin prime pair: both are prime and $13 - 11 = 2$.
  • $(3, 5)$ and $(5, 7)$ are both twin prime pairs; the number $5$ belongs to two pairs.
  • $(7, 11)$ is not a twin prime pair: both are prime, but $11 - 7 = 4$.
  • $(23, 25)$ is not a twin prime pair: $25$ is not prime.

Algorithm

  1. In main, create the object ob of class TwinPrime. The constructor TwinPrime() sets m = 0 and n = 0.
  2. Call ob.input(), which reads the range into the data members m and n.
  3. Call ob.display():
    • If m < 1, n < 1 or m > n, display "Invalid input" and stop.
    • Otherwise display the heading and set count = 0.
    • Repeat for i from m while i + 2 <= n: if isPrime(i) and isPrime(i + 2) are both true, display the pair (i, i + 2) and increase count by $1$.
    • If count is still $0$ after the loop, display "No twin prime pairs found".
  4. The method isPrime(int num) works as follows:
    • Set count = 0.
    • Repeat for i from $1$ to num: if num % i == 0, increase count by $1$.
    • Return true if count == 2, otherwise return false.

Program

The constructor TwinPrime() initialises m and n, input() reads the range, isPrime(int num) returns whether num has exactly two factors, and display() finds and prints the twin prime pairs.

import java.util.Scanner;
public class TwinPrime
{
    int m, n;

    TwinPrime()
    {
        m = 0;
        n = 0;
    }

    void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter m: ");
        m = sc.nextInt();
        System.out.print("Enter n: ");
        n = sc.nextInt();
    }

    boolean isPrime(int num)
    {
        int count = 0;
        for (int i = 1; i <= num; i++)
        {
            if (num % i == 0)
                count++;
        }
        return count == 2;
    }

    void display()
    {
        if (m < 1 || n < 1 || m > n)
        {
            System.out.println("Invalid input");
            return;
        }
        System.out.println("Twin prime pairs between " + m + " and " + n + ":");
        int count = 0;
        for (int i = m; i + 2 <= n; i++)
        {
            if (isPrime(i) && isPrime(i + 2))
            {
                System.out.println("(" + i + ", " + (i + 2) + ")");
                count++;
            }
        }
        if (count == 0)
            System.out.println("No twin prime pairs found");
    }

    public static void main(String args[])
    {
        TwinPrime ob = new TwinPrime();
        ob.input();
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
mintData member; the lower limit of the range
nintData member; the upper limit of the range
scScannerObject used in input() to read the limits
numintParameter of isPrime(); the number being tested
countintIn isPrime(), counts the factors of num; in display(), counts the twin prime pairs found
iintLoop counter; in isPrime() each value from 1 to num is tested as a factor, in display() it is the smaller number of the pair
argsString[]Command line arguments of main; not used
obTwinPrimeObject of the class TwinPrime created in main

Sample input and output

Sample 1

Enter m: 10
Enter n: 20
Twin prime pairs between 10 and 20:
(11, 13)
(17, 19)

Sample 2

Enter m: 24
Enter n: 28
Twin prime pairs between 24 and 28:
No twin prime pairs found

Sample 3

Enter m: 30
Enter n: 10
Invalid input

One value per line

Dry run

Input: m = 10, n = 20. The loop in display() runs for i from $10$ to $18$, because i + 2 <= 20.

iisPrime(i)i + 2isPrime(i + 2)Pair displayed?count
10false12falseNo0
11true13trueYes: (11, 13)1
12false14falseNo1
13true15falseNo1
14false16falseNo1
15false17trueNo1
16false18falseNo1
17true19trueYes: (17, 19)2
18false20falseNo2

The loop ends, count = 2, so "No twin prime pairs found" is not displayed. The output is the pairs $(11, 13)$ and $(17, 19)$, as in the first sample run.

Variations

  1. Range: "Display all prime numbers between m and n."
  2. Emirp number: "Check whether a number is an emirp number: a prime whose reverse is also prime."