-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime_Number.java
More file actions
39 lines (29 loc) · 972 Bytes
/
Copy pathPrime_Number.java
File metadata and controls
39 lines (29 loc) · 972 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
35
36
37
38
39
public class Prime_Number {
public static void main(String[] args) {
System.out.println("The first 50 prime numbers are \n");
printPrimeNumbers(50);
}
public static void printPrimeNumbers(int numberOfPrimes) {
final int NUMBER_OF_PRIMES_PER_LINE = 10;
int count = 0;
int number = 2;
while (count < numberOfPrimes) {
if (isPrime(number)) {
count++;
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
System.out.printf("%-5d\n", number);
} else
System.out.printf("%-5d", number);
}
number++;
}
}
public static boolean isPrime(int number) {
for (int divisor = 2; divisor <= number / 2; divisor++) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
}