Kodekraftt
/Blog
/Twin Prime Numbers In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Twin Prime Numbers in Java — Definition, Examples and Program

Learn how to check for twin prime numbers in Java with clear definition, examples, and a program to find twin primes efficiently.

Twin Prime Numbers in Java — Definition, Examples and Program

Twin prime numbers are pairs of prime numbers that have a difference of exactly 2. In other words, if p and p+2 are both prime, they form a twin prime pair.

Examples of Twin Prime Numbers

  • (3, 5)
  • (5, 7)
  • (11, 13)
  • (17, 19)

How to Check Twin Primes?

  1. Take a number as input.
  2. Check if the number is prime.
  3. Check if the number + 2 is also prime.
  4. If both are prime → They form a twin prime pair.

Java Program to Check Twin Prime Numbers


import java.util.Scanner;

public class TwinPrime {

    // Function to check if a number is prime
    public static boolean isPrime(int num) {
        if (num <= 1) return false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        if (isPrime(num) && isPrime(num + 2)) {
            System.out.println(num + " and " + (num + 2) + " are Twin Prime Numbers");
        } else {
            System.out.println(num + " and " + (num + 2) + " are NOT Twin Prime Numbers");
        }
    }
}

Sample Output


Enter a number: 5
5 and 7 are Twin Prime Numbers

Enter a number: 14
14 and 16 are NOT Twin Prime Numbers

Practice Challenges

  1. Write a program to print all twin prime pairs between 1 and 100.
  2. Modify the program to find twin prime pairs where both numbers are greater than a user-specified number N.
  3. Check if a given number is part of any twin prime pair.

Twin prime numbers are interesting in number theory and are studied for their distribution among prime numbers. This Java program helps you quickly identify twin primes using efficient prime checking.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.