**What is a Prime Number?**

A **Prime Number** is a number which is greater than **1** and divisible by** 1** and only **itself**. Some of the **Prime Numbers** are **2, 3, 5, 7, 11, 13, 17… **In this Prime Number Program in Java, let’s take a look into the different prime number programs.

**Is 0 a prime number?**

**0** is neither **prime** nor **composite** number, as per the definition, a prime number is a number with **exactly two positive divisors**, **1 **and **itself**. **Zero** has an **infinite number of divisors** (we can divide **0** by all real numbers) Therefore, **zero** is not a Prime Number.

**Is 1 a prime number?**

**1** is not considered as a **Prime **because it does not meet the criteria which is exactly two factors **1** and **itself**, whereas **1** has only **one** factor

**Prime Number Program in Java using Scanner**

We all know that the prime numbers can only be divided by **itself** and **1**. Let’s understand the range to consider.

In general, a number cannot be divided by any number which is **greater** than itself and hence we can set the upper limit as to the **number**. Furthermore, we can limit the range by considering the fact that no number can have factors greater than the **square root** of the **number** **(or) number by half** (including the number itself).

For Example, Let’s take the number **19**. It cannot be divided by any number greater than **19**, **20** cannot divide **19** and range to consider is **19/2** which is **9.5** and hence we can consider the range between **2** to **9**.

- Get the number to check from the user.
- Check whether the number is greater than
**1**, if the number is less than**1**then it cannot be a prime. - In an iterative loop, divide the number between the range
**2**to**number/2**, and check if the remainder is not**zero**, if zero then the number is not a prime.

package com.javainterviewpoint; import java.util.Scanner; public class PrimeNumber1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number check :"); int number = scanner.nextInt(); if(checkPrime(number)) System.out.println(number +" is a Prime Number"); else System.out.println(number +" is not a Prime Number"); } public static boolean checkPrime(int n) { if(n <= 1) return false; for(int i=2; i<= n/2; i++) { if((n % i) == 0) return false; } return true; } }

- Using
**Scanner**get the input from the user and store it in the variable**“number”**. - The
**checkPrime()**method, checks whether the number passed is prime or not. It returns boolean values based on the below criteria- Returns
**false**when the number is**less**than or**equal**to**1**. - Returns
**false**when the**remainder**is**zero**. - If the number is greater than
**1**and it is not**divisible**any number within the range of**2**to**number/2**then it returns**true**.

- Returns

**Prime Number Program in Java using While Loop**

We can also use the **while** loop instead of **for** loop, let’s re-write the above code using **while** loop.

We just need to make some minor modifications like the initialization of **“i” [i=2] **happens just before the start of the loop, incrementation of **“i” [i++]** happens inside the loop and of course, we need to change **for** loop into **while** loop.

package com.javainterviewpoint; import java.util.Scanner; public class PrimeNumber2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number check :"); int number = scanner.nextInt(); if(checkPrime(number)) System.out.println(number +" is a Prime Number"); else System.out.println(number +" is not a Prime Number"); } public static boolean checkPrime(int n) { if(n <= 1) return false; int i=2; while(i<= n/2) { if((n % i) == 0) return false; i++; } return true; } }

**Java program to find all Prime Numbers from array**

In this approach, let’s **get** the **input** from the **user** and **store** it in the **array** and find all the prime numbers from array.

- Get the
**size**of the array from the user and create an array to store the input numbers - Get the
**elements**of the array from the user and store it in the array which is created in the previous step - Finally, iterate the array and pass each element to the
**checkPrime()**method and perform the prime validation

package com.javainterviewpoint; import java.util.Arrays; import java.util.Scanner; public class PrimeNumber3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the size of the array :"); int arraySize = scanner.nextInt(); int[] values = new int[arraySize]; System.out.println("Enter the elements of the array : "); for (int i = 0; i < arraySize; i++) { values[i] = scanner.nextInt(); } System.out.println("*** Printing the Prime Numbers ***"); for (int i = 0; i < arraySize; i++) { if (checkPrime(values[i])) { System.out.println(values[i]); } } } public static boolean checkPrime(int n) { if (n <= 1) return false; int i = 2; while (i <= n / 2) { if ((n % i) == 0) return false; i++; } return true; } }

**Output:**

Enter the size of the array : 5 Enter the elements of the array : 1 2 3 4 5 *** Printing the Prime Numbers *** 2 3 5

**Prime Numbers between 1 to 100 / List of Prime Numbers from 1 to 100**

Let’s print all the prime numbers between **1** to **100**

package com.javainterviewpoint; public class PrimeNumber4 { public static void main(String[] args) { System.out.println("*** Prime Numbers between 1 to 100 ***"); for (int i = 2; i < 100; i++) { if (checkPrime(i)) { System.out.print(i+" "); } } } public static boolean checkPrime(int n) { if(n <= 1) return false; int i=2; while(i <= n/2) { if((n % i) == 0) return false; i++; } return true; } }

**Output:**

*** Prime Numbers between 1 to 100 *** 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

## Find all Prime Numbers between 1 to N

- Get the upper limit from the user and store it in the variable
**“N”** - Start the loop from
**2**to**N**, for each iteration increment the loop by**1** - In the
**checkPrime()**method, we have used a boolean flag. It will be set to**false**when the number is less than**1**or if the number is divisible by**number/2.** - Validate the boolean returned by the
**checkPrime()**and print the**number**if the**boolean**returned is**true**.

package com.javainterviewpoint; import java.util.Scanner; public class PrimeNumber5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the Upper limit :"); int N = scanner.nextInt(); System.out.println("*** Prime Numbers between 1 to N ***"); for (int i = 2; i <= N; i++) { if (checkPrime(i)) { System.out.print(i+" "); } } } public static boolean checkPrime(int n) { boolean flag = true; if(n <= 1) flag = false; for(int i=2; i<= n/2; i++) { if((n % i) == 0) { flag = false; break; } } return flag; } }

**Output:**

Enter the Upper limit : 55 *** Prime Numbers between 1 to N *** 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53

**Bonus – Prime Numbers Chart**

Below table contains the list of Prime Numbers from **1** to **100**. All the prime numbers are shaded with a green background.

Happy Learning!!

## Leave a Reply