# Find Twin Primes Number using Java

In this section, you will learn how find primes and twin primes from the first 100 natural numbers.

In this section, you will learn how find primes and twin primes from the first 100 natural numbers.

### Find Twin Primes Number using Java

In this section, you will learn how find primes and twin primes from the first 100 natural numbers. For this, we have used the 'for loop' statement and set the required condition for a prime number. As you already know, prime numbers are the numbers which can only  be divided by 1 and the number itself. Here we are going to find twin primes. Twin Primes are the pair of prime numbers which are having a difference of 2 between two consecutive prime numbers. Following condition will determine the twin primes between 1 to 100:

```if ((i - LastPrime) == 2) {
System.out.println("(" + (i - 2) + "," + i + ")");
}
LastPrime = i;
```

Here is the code:

```class TwinPrimes {
public static void main(String args[]) {
String primeNo = "";
int j = 0;
int LastPrime = 1;
System.out.println("Twin Primes are:");
for (int i = 1; i < 100; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
primeNo += i + " ";
if ((i - LastPrime) == 2) {
System.out.println("("+(i-2)+","+i+")");
}
LastPrime = i;
}
}
System.out.println("Prime Numbers are: " + primeNo);
}
}
```

Output:

```Twin Primes are:
(3,5)
(5,7)
(11,13)
(17,19)
(29,31)
(41,43)
(59,61)
(71,73)
Prime Numbers are: 2 3 5 7 11 13 17 19 13 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
```