## Prime number program in Java

A number is said to be a prime number if it is greater than 1 and is divisible only by 1 and itself. For example the number 2 is divisible by 1 and 2 only. If you take 4 it is divisible by 1, 2 and 4. Hence 4 is not a prime number.

Write a program to check if the given number is prime or not (or) write a program to display Prime numbers from 1 to n (or) write a program to display n prime numbers are important Java interview questions. Let us see the program for these.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public class PrimeNumberCheck { /** * @param args */ public static void main(String[] args) { checkIfPrimeNumber(1); checkIfPrimeNumber(3); checkIfPrimeNumber(11); checkIfPrimeNumber(15); } private static void checkIfPrimeNumber(int n) { boolean prime = true; if (n > 1) { for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { prime = false; break; } } } else { prime = false; } if (prime) { System.out.println(n + " is a prime number"); } else { System.out.println(n + " is not a prime number"); } } } Output: 1 is not a prime number 3 is a prime number 11 is a prime number 15 is not a prime number |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.util.Scanner; public class PrimeNumberDisplay { /** * @param args */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the value of n:"); int n = scanner.nextInt(); scanner.close(); if (n <= 1) { System.out.println("There is no prime number in this range"); } else { System.out.println("Prime numbers from 1 to " + n + " are: "); } for (int i = 2; i <= n; i++) { boolean prime = true; for (int j = 2; j <= i / 2; j++) { if (i % j == 0) { prime = false; break; } } if (prime) { System.out.println(i); } } } } Output: Enter the value of n: 30 Prime numbers from 1 to 30 are: 2 3 5 7 11 13 17 19 23 29 |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import java.util.Scanner; public class NPrimeNumberDisplay { /** * @param args */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the value of n:"); int n = scanner.nextInt(); scanner.close(); if (n <= 1) { System.out.println("There is no prime number in this range"); } else { System.out.println("First " + n + " prime numbers are: "); } int count = 1; for (int i = 2; count <= n; i++) { boolean prime = true; for (int j = 2; j <= i / 2; j++) { if (i % j == 0) { prime = false; break; } } if (prime) { System.out.println(i); count++; } } } } Output: Enter the value of n: 5 First 5 prime numbers are: 2 3 5 7 11 |

Hope you find these programs useful. If there is any error in the code let me know.

The following two tabs change content below.

Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

#### Latest posts by Uday (see all)

- How to run shell commands using SSH connection in Java - July 26, 2017
- Java program to print rectangle of stars - July 20, 2017
- Introduction to Java Enum data type with example programs - March 31, 2017