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 i, j, k = 0, r;// 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 = s; r <= l; r++)// loop to check every number within range { int a[] = new int[4];// array for storing prime numbers for (i = 0; i < 4; i++) { /** This loop is for checking prime numbers in line n,n+2,n+4&n+6 */ for (k = 0, j = 1; j <= (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 = 0; i < 4; i++) 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 = 0; i < 4; i++) if (i != 1) System.out.print(a[i] + "\t"); System.out.println(); } } }}
import java.io.*;
public class prime_triplets { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int i, j, k = 0, r;// 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 = s; r <= l; r++)// loop to check every number within range { int a[] = new int[4];// array for storing prime numbers for (i = 0; i < 4; i++) { /** This loop is for checking prime numbers in line n,n+2,n+4&n+6 */ for (k = 0, j = 1; j <= (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 = 0; i < 4; i++) 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 = 0; i < 4; i++) if (i != 1) System.out.print(a[i] + "\t"); System.out.println(); } } }}
0 Comments