×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Disarium Number CO Prime
Twin Prime - Java
5755    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.
Sri Sri Ramakrishna Paramahamsa
The supreme purpose and goal for human life... is to cultivate love.
Sri Sri Ramakrishna Paramahamsa
2089
63.45
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     46744
25/06/2018     38887
01/01/2018     38546
28/06/2017     36472
02/08/2017     35190
01/08/2017     29444
06/07/2017     29200
15/05/2017     28721
14/07/2017     24570
11/09/2018     23623
softetechnologies