Java Program to Check Twin Prime Number

Program to Check Twin Prime Number

In this article we are going to understand what Twin Prime number is and how we can check whether a number is Twin Prime or not in Java with examples.

Twin Primes are prime numbers which are prime number that is either 2 less or 2 more than another prime number.

 Example :

(41,43) Twin Prime number
(67,60 )Not Twin Prime number
(99,33 ) Not Twin Prime number

In the above examples the numbers 41,43 are only twin primes. In the second set 60 is not a prime number and in the third 33 is not a prime number. Hence they are not twin primes.

Let’s see different ways to check twin prime number.

Approach :

  1. Enter/declare two numbers and store them in num1 and num2.
  2. Then, we check if both the numbers are prime numbers and the absolute difference between them is 2.

Method-1: By Using Static Value

import java.util.Scanner;

public class TwinPrimeNumber
{
    public static void main(String args[])
    {
        //first number declared
        int num1 = 41;
        //second number declared
        int num2 =43;

        //Logic to check for coprimes
        if (prime(num1) && prime(num2) && (Math.abs(num1 - num2) == 2))
        {
            System.out.println(num1+" and "+num2+" are Twin Primes");
        }
        else
        {
            System.out.println(num1+" and "+num2+" are Not Twin Primes");
        }
    }

    //Function to check for prime
    static boolean prime(int num)
    {
        int iter = 2;
        boolean flag = true;
        while (num > iter)
        {
            if (num % iter == 0)
            {
                flag = false;
                break;
            }
            iter++;
        }
        return flag;
    }
}
Output:

41 and 43 are Twin Primes

Method-2: By User Input Value

import java.util.Scanner;

public class TwinPrimeNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter first number : ");
        int num1 = scan.nextInt();
        System.out.print("Enter second number : ");
        int num2 = scan.nextInt();

        //Logic to check for coprimes
        if (prime(num1) && prime(num2) && (Math.abs(num1 - num2) == 2))
        {
            System.out.println(num1+" and "+num2+" are Twin Primes");
        }
        else
        {
            System.out.println(num1+" and "+num2+" are Not Twin Primes");
        }
    }

    //Function to check for prime
    static boolean prime(int num)
    {
        int iter = 2;
        boolean flag = true;
        while (num > iter)
        {
            if (num % iter == 0)
            {
                flag = false;
                break;
            }
            iter++;
        }
        return flag;
    }
}
Output:

Case-1

Enter first number : 41
Enter second number : 43 
41 and 43 are Twin Primes

Case-2

Enter first number : 41
Enter second number : 47
41 and 47 are Not Twin Primes

Leave a Comment