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}
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}}
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}
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
1import java.util.Scanner;
2
3public class PrimeNumberChecker {
4 public static void main(String[] args) {
5 System.out.println("Enter a number to check Prime or Not");
6 Scanner scanner = new Scanner(System.in);
7 int number = scanner.nextInt();
8 int i = 2, count = 0;
9 while (i <= number / 2) {
10 if (number % i == 0) {
11 count++;
12 break;
13 }
14 i++;
15 }
16 if (count == 0) {
17 System.out.println(number + " is prime number");
18 } else {
19 System.out.println(number + " is not a prime number");
20 }
21 }
22}
1import java.util.ArrayList;
2import java.util.List;
3import java.util.Scanner;
4
5public class PrimeNumberChecker3 {
6
7 public static List<Integer> generatePrimeInRange(int startRange, int endRange) {
8 if (startRange >= endRange) {
9 System.out.println("endRange must be greater then startRange");
10 System.exit(0);
11 }
12 List<Integer> primeList = new ArrayList<>();
13 while (startRange <= endRange) {
14 int i = 2, count = 0;
15 while (i <= startRange / 2) {
16 if (startRange % i == 0) {
17 count++;
18 break;
19 }
20 i++;
21
22 }
23 if (count == 0) {
24 primeList.add(startRange);
25 }
26 startRange++;
27 }
28 return primeList;
29 }
30
31 public static void main(String[] args) {
32 System.out.println("Enter a number range to generate prime numbers in between");
33 Scanner scanner = new Scanner(System.in);
34 int number1 = scanner.nextInt();
35 int number2 = scanner.nextInt();
36 List<Integer> primeNumbers = generatePrimeInRange(number1, number2);
37 System.out.println("Prime Numbers are");
38 System.out.println(primeNumbers);
39
40 }
41}