Page Contents
A prime number is a number greater than 1 and divided by 1 or by itself. That is, you cannot divide a prime number by any number other than 1 or itself. For example, 2, 3, 5, 7, 11, 13, 17, … etc.
Example – Prime numbers between a given range
public class PrimeNumber { public static void main(String[] args) { int first = 0; int last = 50; System.out.println("Number of prime numbers between range " + first + " and " + last); for (int i = first; i <= last; i++) { if (isPrime(i)) { System.out.print(i + " "); } } } public static boolean isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } }
Output
Number of prime numbers between range 0 and 50 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Example – Check if a given number is prime or not using for loop
public class PrimeNumberCheker { public static void main(String[] args) { int number = 15; boolean flag = false; for (int i = 2; i <= number / 2; ++i) { // condition for nonprime number if (number % i == 0) { flag = true; break; } } if (!flag) { System.out.println(number + " is a prime number."); } else { System.out.println(number + " is not a prime number."); } } }
Output
15 is not a prime number.
Example – Print all prime numbers in a given range using for loop
public class PrimeNumberCheker { public static void main(String[] args) { for(int number=2; number<=50; number++){ // 2 to 50 range boolean flag = false; for (int i = 2; i <= number / 2; ++i) { // condition for nonprime number if (number % i == 0) { flag = true; break; } } if (!flag) { System.out.println(number + " is a prime number."); } else { System.out.println(number + " is not a prime number."); } } } }
Output
2 is a prime number. 3 is a prime number. 4 is not a prime number. 5 is a prime number. 6 is not a prime number. 7 is a prime number. 8 is not a prime number. 9 is not a prime number. 10 is not a prime number. 11 is a prime number. 12 is not a prime number. 13 is a prime number. 14 is not a prime number. 15 is not a prime number. 16 is not a prime number. 17 is a prime number. 18 is not a prime number. 19 is a prime number. 20 is not a prime number. 21 is not a prime number. 22 is not a prime number. 23 is a prime number. 24 is not a prime number. 25 is not a prime number. 26 is not a prime number. 27 is not a prime number. 28 is not a prime number. 29 is a prime number. 30 is not a prime number. 31 is a prime number. 32 is not a prime number. 33 is not a prime number. 34 is not a prime number. 35 is not a prime number. 36 is not a prime number. 37 is a prime number. 38 is not a prime number. 39 is not a prime number. 40 is not a prime number. 41 is a prime number. 42 is not a prime number. 43 is a prime number. 44 is not a prime number. 45 is not a prime number. 46 is not a prime number. 47 is a prime number. 48 is not a prime number. 49 is not a prime number. 50 is not a prime number.
Example – Check if a given number is prime or not using while loop
public class PrimeNumberCheker { public static void main(String[] args) { int number = 13; //Given Number int i = 2; boolean flag = false; while (i <= number / 2) { if (number % i == 0) { flag = true; break; } i=i+1; } if (!flag) { System.out.println(number + " is a prime number."); } else { System.out.println(number + " is not a prime number."); } } }
Output
13 is a prime number.