Skip to main content
Browse topics

Twin Prime Example in Java

2 min read Updated May 29, 2026
Share

Introduction

Twin Prime is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

A twin prime is a prime number that has a prime gap of two. In other words, to qualify as a twin prime, the prime number must be either 2 less or 2 more than another prime number (which by definition would mean that it, too, is a twin prime)?for example, the twin prime pair (41, 43). Two is not considered a twin prime, since it violates the aforementioned rule.

Twin Prime Example Program

java
import java.util.Scanner;
public class TwinPrime {
    public static void main(String a[]){
        System.out.print("How many twin primes do you need?");
        Scanner in= new Scanner(System.in);
        int input = in.nextInt();
        int i=3,n=35;
        boolean b,task=true;
        int count =0;
        while(task){
            if((isPrime(i)) & ( isPrime(i+2))){   
                count++;
                System.out.println("  "+(i-2)+"  "+i );
                if(count==input ){
					task = false; }
            }
                    i+=2;
        }
	}
    public static boolean isPrime(int n) {
    if (n%2==0) return false;//check if n is a multiple of 2
    //if not, then just check the odds
    for(int i=3;i*i<=n;i+=2){
        if(n%i==0)
        return false;
    }
    return true;
	}
}

Sample Output

plaintext
How many twin primes do you need? 8
  1  3
  3  5
  9  11
  15  17
  27  29
  39  41
  57  59
  69  71

When to use

Use this twin prime example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.Scanner; imports a class used later in the program.

  3. A println / print call writes text to the console — part of the sample output below.

  4. A Scanner reads typed input from the keyboard (System.in).

  5. int input = in.nextInt(); updates a variable used in the calculation or output.

  6. int i=3,n=35; updates a variable used in the calculation or output.

  7. boolean b,task=true; updates a variable used in the calculation or output.

  8. A loop repeats the block until its condition becomes false.

Best Practices

Common Mistakes

Frequently Asked Questions

What does the Twin Prime program demonstrate?
It shows how to implement twin prime in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related tutorials

Search tutorials