×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Disarium Number CO Prime
Twin Prime - Java
4921    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.
Rabindranath Tagore
The biggest changes in a women's nature are brought by love; in man, by ambition
Rabindranath Tagore
425
50.7
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     41364
01/01/2018     35770
28/06/2017     33774
25/06/2018     32831
02/08/2017     32142
06/07/2017     26594
01/08/2017     26559
15/05/2017     26214
14/07/2017     21527
21/04/2018     20512
softetechnologies