-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimesInRange.java
More file actions
38 lines (29 loc) · 1 KB
/
Copy pathPrimesInRange.java
File metadata and controls
38 lines (29 loc) · 1 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
package CodingPractice;
import java.util.Scanner;
public class PrimesInRange {
public static void main(String[] args) {
// Print all prime numbers in a given range (Inclusive)
Scanner sc = new Scanner(System.in);
System.out.println("Enter lower range : ");
int lowerRange = sc.nextInt();
System.out.println("Enter upper range : ");
int upperRange = sc.nextInt();
if (lowerRange < 0 || upperRange < 0) {
System.out.println("Invalid Inputs. Enter Positive Numbers only.");
} else {
for (int i = lowerRange; i <= upperRange; i++) {
int flag = 1;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (i != 0 && i != 1 && flag == 1) {
System.out.print(i + " ");
}
}
}
sc.close();
}
}