Dremendo Tag Line

Print all pairs of two digit twin prime numbers using for loop in Java

for Loop - Question 11

In this question, we will see how to print all pairs of two-digit twin-prime numbers in Java programming using for loop. To know more about for loop click on the for loop lesson.

Q11) Write a program in Java to print all pairs of two-digit twin-prime numbers using for loop.

A twin-prime numbers are numbers that differs from another prime number by two. Examples of two-digit twin-prime numbers are: (11, 13), (17, 19), (29, 31), (41, 43), etc.

Program

public class Q11
{
    public static void main(String args[])
    {
        int i,j,fc,n;
        System.out.println("List of all twin prime numbers between 10 to 99");
        for(i=10; i<=99; i=i+1)
        {
            fc=0;
            for(j=1; j<=i; j=j+1)
            {
                if(i%j==0)
                {
                    fc=fc+1;		// counting the factors of i (value of i)
                }
            }

            if(fc==2)
            {
                fc=0;
                n=i+2;		// taking another number from the current value of i with a difference of 2

                for(j=1; j<=n; j=j+1)
                {
                    if(n%j==0)
                    {
                        fc=fc+1;		// counting the factors of n (value of n)
                    }
                }

                if(fc==2)
                {
                    System.out.println("(" + i + ", " + n + ")");
                }
            }
        }
    }
}

Output

List of all twin prime numbers between 10 to 99
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
video-poster