1public class Main {
2 public static void main(String[] args) {
3 for (int i = 2; i < 1_000_000; ++i) {
4 boolean isPrime = true;
5 int sqrt = (int)Math.ceil(Math.sqrt(i));
6 for (int divisor = 2; divisor <= sqrt; ++divisor) {
7 if (i % divisor == 0) {
8 isPrime = false;
9 break;
10 }
11 }
12 if (isPrime) {
13 System.out.println(i);
14 }
15 }
16 }
17}
1public class Prime {
2 public static void main(String[] args) {
3
4 int num = 29;
5 boolean flag = false;
6 for(int i = 2; i <= num/2; ++i){
7 if(num % i == 0){
8 flag = true;
9 break;
10 }
11 }
12
13 if (!flag)
14 System.out.println(num + " is a prime number.");
15 else
16 System.out.println(num + " is not a prime number.");
17}}
1public class Prime {
2 public static void main(String[] args) {
3
4 int num = 29;
5 boolean flag = false;
6 for(int i = 2; i <= num/2; ++i){
7 if(num % i == 0){
8 flag = true;
9 break;
10 }
11 }
12
13 if (!flag)
14 System.out.println(num + " is a prime number.");
15 else
16 System.out.println(num + " is not a prime number.");
17}}
18Source:www.progra
19
1public class Prime {
2
3 public static void main(String[] args) {
4
5 int num = 29;
6 boolean flag = false;
7 for(int i = 2; i <= num/2; ++i)
8 {
9 // condition for nonprime number
10 if(num % i == 0)
11 {
12 flag = true;
13 break;
14 }
15 }
16
17 if (!flag)
18 System.out.println(num + " is a prime number.");
19 else
20 System.out.println(num + " is not a prime number.");
21 }
22}
1It will display the prime numbers between 1 and 100.
2
3class PrimeNumbers
4{
5 public static void main (String[] args)
6 {
7 int i =0;
8 int num =0;
9 //Empty String
10 String primeNumbers = "";
11
12 for (i = 1; i <= 100; i++)
13 {
14 int counter=0;
15 for(num =i; num>=1; num--)
16 {
17 if(i%num==0)
18 {
19 counter = counter + 1;
20 }
21 }
22 if (counter ==2)
23 {
24 //Appended the Prime number to the String
25 primeNumbers = primeNumbers + i + " ";
26 }
27 }
28 System.out.println("Prime numbers from 1 to 100 are :");
29 System.out.println(primeNumbers);
30 }
31}