-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwinPrime.java
More file actions
49 lines (48 loc) · 1.59 KB
/
TwinPrime.java
File metadata and controls
49 lines (48 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
public class TwinPrime {
public static void main(String args[]) {
Scanner inp = new Scanner(System.in);
System.out.print("Enter the lower value:");
int iLower = inp.nextInt();
System.out.print("Enter the upper value:");
int iUpper = inp.nextInt();
if (iLower < 0 || iUpper < 0) {
System.out.println("Entered Value is -ve, Can't find Twin Prime");
System.exit(99);
}
if(iLower>iUpper){
System.out.println("Input Range is Invalid!\nThe lower value is greater than the upper");
System.out.println("Do you want to swap the values: 1: yes, 0: no");
int iChoice = inp.nextInt();
if(iChoice == 1){
int iTemp = iLower;
iLower = iUpper;
iUpper = iTemp;
}
else if(iChoice == 0){
System.out.println("Exit");
System.exit(99);
}
else
System.exit(99);
}
System.out.print("The prime numbers are:");
for (int val = iLower; val < iUpper; val++) {
int flag = 0;
int value = (val + 2);
if (val == 1)
flag = 0;
if (val == 2)
flag = 1;
for (int u = 2; u <= val / 2; u++) {
if (val % u == 0 || value % u == 0) {
flag = 1;
break;
}
}
if (flag == 0)
System.out.print("(" + val + "," + value + ")" + ",");
}
inp.close();
}
}