# Prime Number Finder In Java

A prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself.
This small program runs through 2-500 and print all the prime numbers in that range.

Note: The number 1 is by definition not a prime number.

```package com.kushal.utils;

/**
* Method to test whether a number is
* a prime number or not
*/
public static boolean isPrimeNumber(int number) {
boolean prime = true;
int limit = (int) Math.sqrt(number);

for (int i = 2; i <= limit; i++) {
if (number % i == 0) {
prime = false;
break;
}
}

return prime;
}

public static void main(String[] args) {

/**
* Loop From 2-500 and Print all the prime numbers
*/
System.out.println("List Of Prime Numbers From 2-500");
for (int i = 2; i <= 500; i++) {
{
System.out.println(i);
}
}
}
}
```

———————————
Here is the output of this program:

```List Of Prime Numbers From 2-500
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
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293
307
311
313
317
331
337
347
349
353
359
367
373
379
383
389
397
401
409
419
421
431
433
439
443
449
457
461
463
467
479
487
491
499
```

[My Video Promotion]

### 3 Responses to Finding Mean Value Of An Integer Array In Java

1. Jerry says:

we need the the square root of the number, not the number itself.
squareRoot = midValue * midValue;

if (squareRoot == number) {
return squareRoot;

2. Sabrina Kundu says:

Hi Kushal,

This program works for some cases, but not all. For example, 4 prints out 4.0 when it is supposed to print out 2.0. Fortunately, I found the mistake. If you change “if (squareRoot == number) return squareRoot;” to “if(squareRoot == number) return midValue;”, then the program prints out the correct square root for even numbers.

Thanks,

Sabrina

• icodejava says:

Thank you Sabrina for the feedback.

This site uses Akismet to reduce spam. Learn how your comment data is processed.