1// Java program to print prime numbers in a given range
2public class PrimeNumberRangeDemo
3{
4 public static void main(String[] args)
5 {
6 int lowerRange = 10, higherRange = 50;
7 while(lowerRange < higherRange)
8 {
9 boolean flag = false;
10 for(int a = 2; a <= lowerRange / 2; ++a)
11 {
12 if(lowerRange % a == 0)
13 {
14 flag = true;
15 break;
16 }
17 }
18 if(!flag && lowerRange != 0 && lowerRange != 1)
19 System.out.print(lowerRange + " ");
20 ++lowerRange;
21 }
22 }
23}
1// Java program to print prime numbers from 2 to n
2import java.util.Scanner;
3public class PrimeNumberFrom2Ton
4{
5 public static void main(String[] args)
6 {
7 int loop, max;
8 System.out.print("Please enter highest (maximum) number: ");
9 Scanner sc = new Scanner(System.in);
10 max = sc.nextInt();
11 for(loop = 2; loop < max; ++loop)
12 {
13 if(findPrime(loop))
14 {
15 System.out.println(loop);
16 }
17 }
18 sc.close();
19 }
20 public static boolean findPrime(int number)
21 {
22 int a;
23 boolean boolPrime = true;
24 for(a = 2; a < number / 2; a++)
25 {
26 if(number%a == 0)
27 {
28 boolPrime = false;
29 break;
30 }
31 }
32 return boolPrime;
33 }
34}
1Now let’s see print first 10 prime numbers in java.
2
3public class FirstTenPrimeNumbers
4{
5 public static void main(String[] args)
6 {
7 int count = 0, number = 0, a = 1, b = 1;
8 while(number < 10)
9 {
10 b = 1;
11 count = 0;
12 while(b <= a)
13 {
14 if(a%b == 0)
15 count++;
16 b++;
17 }
18 if(count == 2)
19 {
20 System.out.printf("%d ", a);
21 number++;
22 }
23 a++;
24 }
25 }
26}
1// Java program to print prime numbers upto n
2import java.util.Scanner;
3public class PrimeNumberUptoN
4{
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in);
8 int a = 0;
9 int n = 0;
10 String strPrime = "";
11 System.out.println("Please enter the value of n: ");
12 int number = sc.nextInt();
13 for(a = 1; a <= number; a++)
14 {
15 int counter = 0;
16 for(n = a; n >= 1; n--)
17 {
18 if(a % n == 0)
19 {
20 counter = counter + 1;
21 }
22 }
23 if(counter == 2)
24 {
25 strPrime = strPrime + a + " ";
26 }
27 }
28 System.out.println("prime numbers upto n are: ");
29 System.out.println(strPrime);
30 sc.close();
31 }
32}
1class PrimeNumbers
2{
3 public static void main (String[] args)
4 {
5 int i =0;
6 int num =0;
7 //Empty String
8 String primeNumbers = "";
9
10 for (i = 1; i <= 100; i++)
11 {
12 int counter=0;
13 for(num =i; num>=1; num--)
14 {
15 if(i%num==0)
16 {
17 counter = counter + 1;
18 }
19 }
20 if (counter ==2)
21 {
22 //Appended the Prime number to the String
23 primeNumbers = primeNumbers + i + " ";
24 }
25 }
26 System.out.println("Prime numbers from 1 to 100 are :");
27 System.out.println(primeNumbers);
28 }
29}
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}