# Twin Prime Program in Java

## A twin prime is a prime number that is either 2 less or 2 more than another prime number—for example, either member of the twin prime pair (41, 43). In other words, a twin prime is a prime that has a prime gap of two.

```
import java.util.Scanner;
public class TwinPrime
{
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
if (prime(a) && prime(b) && (Math.abs(a - b) == 2))
{
System.out.println("Twin Prime");
}
else
{
System.out.println("Not Twin Prime");
}
}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}
```

#### Output:

Enter a=5 Enter b=7 Twin Primeb. tech. bca icse java java tutorials learn java mca programs

### Frequently Asked Questions

##### What is Twin Prime ?

A twin prime is a prime number that is either 2 less or 2 more than another prime number—for example, either member of the twin prime pair (41, 43). In other words, a twin prime is a prime that has a prime gap of two.

##### What is Twin Prime in Java?

A twin prime is a prime number that is either 2 less or 2 more than another prime number—for example, either member of the twin prime pair (41, 43). In other words, a twin prime is a prime that has a prime gap of two.

**Recent Posts**

- Prefix increment/decrement Program in C April 11, 2021
- Understand the floating point arithmetic operations Program in C April 11, 2021
- Integer arithmetic operations Program in C April 4, 2021
- Find out the size and limits of data types Program in C April 4, 2021
- ICSE - Computer Applications - for Class 10 - 2019 Solved January 4, 2021
- Selection Sort in String Program in Java January 4, 2021
- Amicable Numbers Program in Java December 24, 2020
- Java Constructors October 1, 2020
- Java String Function replace() July 26, 2020
- Java String Function length() July 26, 2020
- Java String Function lastIndexOf() July 26, 2020
- Java String Function join() July 26, 2020
- Java String Function isEmpty() July 26, 2020
- Java String Function intern() July 26, 2020
- Java String Function getChars() July 26, 2020
- Java String Function getBytes() July 26, 2020
- Java String Function format() July 26, 2020
- Java String function equalsIgnoreCase() July 25, 2020
- Java String Function equals() July 25, 2020
- Java String function contains() July 25, 2020