forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeRangeFinder.java
More file actions
38 lines (31 loc) · 1.04 KB
/
PrimeRangeFinder.java
File metadata and controls
38 lines (31 loc) · 1.04 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
// filename: PrimeRangeFinder.java
// Compile: javac PrimeRangeFinder.java
// Run: java PrimeRangeFinder
import java.util.*;
public class PrimeRangeFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("=== Prime Range Finder ===");
System.out.print("Enter start of range: ");
int start = sc.nextInt();
System.out.print("Enter end of range: ");
int end = sc.nextInt();
System.out.println("\nPrime numbers between " + start + " and " + end + ":");
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
System.out.println("\n=== End ===");
sc.close();
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0) return false;
}
return true;
}
}