forked from dharmanshu1921/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeNumbers.java
More file actions
Latest commit
35 lines (30 loc) · 687 Bytes
/
Copy pathPrimeNumbers.java
File metadata and controls
35 lines (30 loc) · 687 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
// Using Sieve method
importjava.util.Scanner;
importjava.util.Arrays;
classPrimeNumbers {
privatestaticIntegersieve(Integern) {
if (n <= 1) return0;
Boolean[] arr = newBoolean[n+1];
Arrays.fill(arr, true);
Integercnt = 0;
for (Integeri=2; i<=n; i++) {
if (arr[i] == true) {
cnt++;
System.out.println(i);
for (Integerj=2*i; j<=n; j+=i) {
arr[j] = false;
}
}
}
returncnt;
}
publicstaticvoidmain(String[] args) {
Scannerinp = newScanner(System.in);
Integern = inp.nextInt();
System.out.println();
Integercnt = sieve(n);
System.out.print("Total Prime Numbers: ");
System.out.println(cnt);
inp.close();
}
}