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
- In
main, create the objectobof classTwinPrime. The constructorTwinPrime()setsm = 0andn = 0. - Call
ob.input(), which reads the range into the data membersmandn. - Call
ob.display():- If
m < 1,n < 1orm > n, display "Invalid input" and stop. - Otherwise display the heading and set
count = 0. - Repeat for
ifrommwhilei + 2 <= n: ifisPrime(i)andisPrime(i + 2)are both true, display the pair(i, i + 2)and increasecountby $1$. - If
countis still $0$ after the loop, display "No twin prime pairs found".
- If
- The method
isPrime(int num)works as follows:- Set
count = 0. - Repeat for
ifrom $1$ tonum: ifnum % i == 0, increasecountby $1$. - Return
trueifcount == 2, otherwise returnfalse.
- Set
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
| Variable | Data Type | Purpose |
|---|---|---|
| m | int | Data member; the lower limit of the range |
| n | int | Data member; the upper limit of the range |
| sc | Scanner | Object used in input() to read the limits |
| num | int | Parameter of isPrime(); the number being tested |
| count | int | In isPrime(), counts the factors of num; in display(), counts the twin prime pairs found |
| i | int | Loop counter; in isPrime() each value from 1 to num is tested as a factor, in display() it is the smaller number of the pair |
| args | String[] | Command line arguments of main; not used |
| ob | TwinPrime | Object 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 foundSample 3
Enter m: 30
Enter n: 10
Invalid inputDry run
Input: m = 10, n = 20. The loop in display() runs for i from $10$ to $18$, because i + 2 <= 20.
| i | isPrime(i) | i + 2 | isPrime(i + 2) | Pair displayed? | count |
|---|---|---|---|---|---|
| 10 | false | 12 | false | No | 0 |
| 11 | true | 13 | true | Yes: (11, 13) | 1 |
| 12 | false | 14 | false | No | 1 |
| 13 | true | 15 | false | No | 1 |
| 14 | false | 16 | false | No | 1 |
| 15 | false | 17 | true | No | 1 |
| 16 | false | 18 | false | No | 1 |
| 17 | true | 19 | true | Yes: (17, 19) | 2 |
| 18 | false | 20 | false | No | 2 |
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
- Range: "Display all prime numbers between m and n."
- Emirp number: "Check whether a number is an emirp number: a prime whose reverse is also prime."