-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeNumberRange.java
More file actions
34 lines (30 loc) · 1019 Bytes
/
PrimeNumberRange.java
File metadata and controls
34 lines (30 loc) · 1019 Bytes
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
// Write a program to find out series of prime numbers from a given range, where
// the range is entered by user
import java.util.Scanner;
public class PrimeNumberRange {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the starting number of the range: ");
int start = sc.nextInt();
System.out.print("Enter the ending number of the range: ");
int end = sc.nextInt();
System.out.println("Prime numbers in the given range are:");
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
}