-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeNumbers.java
More file actions
34 lines (32 loc) · 908 Bytes
/
Copy pathPrimeNumbers.java
File metadata and controls
34 lines (32 loc) · 908 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
/* Exercise 5.20
* (Display prime numbers between 2 and 1,000) Modify Listing 5.15 to display all
* the prime numbers between 2 and 1,000, inclusive. Display eight prime numbers
* per line. Numbers are separated by exactly one space
*/
public class PrimeNumbers {
public static void main(String[] args) {
// Calculating prime numbers between 2 and 1000
int count = 0;
int i = 2;
while (i <= 1000) {
boolean prime = true;
// Logic for prime numbers
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
prime = false;
break;
}
}
// Display results
if (prime) {
if (count%8 == 0) {
System.out.println(i);
}
else
System.out.print(i + " ");
count++;
}
i++;
}
}
}