×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Disarium Number CO Prime
Twin Prime - Java
6624    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.
Albert Einstein
Unthinking respect for authority is the greatest enemy of truth.
Albert Einstein
2754
85.81
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     54901
25/06/2018     46029
01/01/2018     44174
28/06/2017     41745
02/08/2017     40825
01/08/2017     34830
06/07/2017     34587
15/05/2017     33799
11/09/2018     31478
14/07/2017     30699
softetechnologies