Java Program To Print Prime Numbers
Chapter:
Interview Programs
Last Updated:
19-07-2016 13:41:24 UTC
Program:
/* ............... START ............... */
import java.util.Scanner;
public class JavaPrimeNumbers {
public static void main(String[] args) {
int num = 50;
int count = 0;
for (int i = 2; i <= num; i++) {
count = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
count++;
break;
}
}
if (count == 0) {
System.out.println(i);
}
}
}
}
/* ............... END ............... */
Output
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
Notes:
-
Prime number is a number that is greater than 1 and divided by 1 or itself.
- In other words, prime numbers can't be divided by other numbers than itself or 1.
- For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.
Tags
Prime Numbers, Java