-
Notifications
You must be signed in to change notification settings - Fork 57
/
PrimeNumberSeries.java
44 lines (40 loc) · 915 Bytes
/
PrimeNumberSeries.java
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
40
41
42
43
44
package com.java.series;
/*
* Prime Number Series
* --------------------
* 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ....
*
* Prime Number
* ------------
* A prime number is a natural number greater than 1
* that is not a product of two smaller natural numbers.
*
* A natural number thats divisible only by itself and 1.
*
*/
public class PrimeNumberSeries {
public static void main(String[] args) {
int n = 100;
System.out.println("Prime numbers up to : "+n);
for(int i=2; i<=n; i++)
if(isPrime(i))
System.out.print(i+", ");
}
public static boolean isPrime(int num){
for(int i=2;i<=Math.sqrt(num);i++)
if(num % i == 0)
return false;
return true;
}
}
/*
OUTPUT
n = 20
Prime numbers up to : 20
2, 3, 5, 7, 11, 13, 17, 19
OUTPUT
n = 100
Prime numbers up to : 100
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
*/