Skip to main content

Program in java : To display all the twin prime numbers between 1 and 100

Program in java : To display all the twin prime numbers between 1 and 100


import java.util.Scanner;
class twinprime {
 public static void main(String[] args) {
  System.out.println("Twin primes between 1 and 100 are ");
  for (int i = 1; i <= 100; i++) {
   int a = i;
   int b = i + 2;
   // c1 for count of value a and c2 is for count of value b
   int c1 = 0, c2 = 0;
   for (int j = 1; j <= a; j++) {
    if (a % j == 0)
     c1++;
   }
   for (int j = 1; j <= b; j++) {
    if (b % j == 0)
     c2++;
   }
   // checking if both the counters are 2
   if (c1 == 2 && c2 == 2) {
    System.out.println(a + " and " + b + " are Twin Prime");
   }
  }
 }
}

Popular posts from this blog