Ticker

6/recent/ticker-posts

Program in JAVA to find prime triplets combinations

Question: Write a program to input start limit S(>0) and last limit L(>0).Print all prime triplets between s and l with a suitable message. Also, print the total number of prime triplets combinations at the end.

Code:



import java.io.*;

public class prime_triplets {
    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int ijk = 0r;// Initialising variables
        System.out.print("S=");
        int s = Integer.parseInt(in.readLine());// Accepting and Converting String To Integer
        System.out.print("L=");
        int l = Integer.parseInt(in.readLine());// Accepting and Converting String To Integer
        for (r = sr <= lr++)// loop to check every number within range
        {
            int a[] = new int[4];// array for storing prime numbers
            for (i = 0i < 4i++) {
                /** This loop is for checking prime numbers in line n,n+2,n+4&n+6 */
                for (k = 0j = 1j <= (2 * i + r); j++)// loop for checking prime number
                    if ((2 * i + r) % j == 0)
                        k = k + 1;
                if (k == 2)// If number is prime then store it in array a
                    a[i] = r + 2 * i;
            }
            if (a[0] != 0 && a[1] != 0 && a[3] != 0// Prime Triplets(n,n+2,n+6)
            {
                for (i = 0i < 4i++)
                    if (i != 2)
                        System.out.print(a[i] + "\t");// Displaying Prime Triplets(n,n+2,n+6)
                System.out.println();
            }
            if (a[0] != 0 && a[2] != 0 && a[3] != 0// Displaying Prime Triplets(n,n+4,n+6)
            {
                for (i = 0i < 4i++)
                    if (i != 1)
                        System.out.print(a[i] + "\t");
                System.out.println();
            }
        }
    }
}


Output Window:

Post a Comment

0 Comments