×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Disarium Number CO Prime
Twin Prime - Java
5459    Arnab De    12/12/2018

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)

softetechnologies
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));
            }
        }
    }
}

softetechnologies
Disarium Number CO Prime
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Swami Vivekananda
All that man has to do is to take care of three things; good thought, good word, good deed.
Swami Vivekananda
1936
58.5
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     43984
01/01/2018     36610
25/06/2018     35913
28/06/2017     34654
02/08/2017     33153
01/08/2017     27616
06/07/2017     27336
15/05/2017     26986
14/07/2017     22627
21/04/2018     21239
softetechnologies