Chapter: Miscellaneous Last Updated: 18-07-2016 04:43:55 UTC

Program:

` ````
/* ............... START ............... */
import java.util.Scanner;
public class JavaPrimeNumbers {
public static void main(String args[]) {
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of prime numbers you want");
n = in.nextInt();
if (n >= 1) {
System.out.println("First " + n + " prime numbers are :-");
System.out.println(2);
}
for (int count = 2; count <= n;) {
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
status = 0;
break;
}
}
if (status != 0) {
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}
/* ............... END ............... */
```

Output

Enter the number of prime numbers you want 10 First 10 prime numbers are :- 2 3 5 7 11 13 17 19 23 29

Notes:

- A prime number (or a prime) is a natural number greater than one that has no positive divisors other than one and itself.
- For example 5 is prime, as only 1 and 5 can divide it.

Tags

Prime Numbers, Java