Print all tweens primes no between 3 and 100 using java programming.
Tweens prime: If a no n is a prime no and (n+2) is a prime no then n and (n+2) are known as tweens prime. Run a loop for 3 to 98 and check if the number n and (n+2) is a prime or not. For that reason, we have to write a method IsPrime(), which return true if the no is a prime no else return false. In other words, twin prime are those numbers which are prime and having a difference of two (2) between them.
For Example: 29 and 31
29 and 31 both are prime numbers and they are having a difference of two (2) between them i.e. 31 - 29 = 2
8 pair of Twin Prime Numbers found from 3 to 100
(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73)
public class TweensPrime { public boolean IsPrime(int i) { boolean b=true; int d=2; while(d<i/2) { if (i%d==0) { b=false; break; } d++; } return b; } public static void main(String []args) { int i,r1=3,r2=100; TweensPrime tp=new TweensPrime(); for(i=r1;i<=r2;i++) { if(tp.IsPrime(i) && tp.IsPrime(i+2)) { System.out.println(i + "-" + (i+2)); } } } }