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}
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}
1public class PrimeNumbersFrom1To100
2{
3 public static void main(String[] args)
4 {
5 int maxNumber = 100;
6 boolean prime = true;
7 String primeNumbers = "";
8 for(int a = 1; a <= maxNumber; a++)
9 {
10 prime = checkPrime(a);
11 if(prime)
12 {
13 primeNumbers = primeNumbers + a + " ";
14 }
15 }
16 System.out.println("Display prime numbers from 1 to " + maxNumber + ": ");
17 System.out.println(primeNumbers);
18 }
19 public static boolean checkPrime(int number)
20 {
21 int remainder;
22 for(int a = 2; a <= number / 2; a++)
23 {
24 remainder = number % a;
25 // if remainder is equal to 0 then number is not prime
26 if(remainder == 0)
27 {
28 return false;
29 }
30 }
31 return true;
32 }
33}