## Introduction to Prime Numbers in Java

This post is about prime numbers in Java. A prime number is a kind of number which is divisible only by unity and the number itself. It is not divisible by any other number. Prime numbers are special kinds of numbers. The exception of numbers is 1 and 2. 1 is the only number that is neither prime nor composite. 2 is the only even number prime in nature. The opposite of prime numbers are numbers that are divisible by numbers other than the number itself as well as unity. Composite numbers and prime numbers are opposite to each other. Generally, prime numbers are odd numbers except for the number 2. It does not necessarily mean that an odd number is always a prime number because it might be divisible by 3 and any other odd number.

### Examples of Prime Numbers in Java

Below are the examples of implementing prime numbers in java:

#### Example #1 – Using For-Loop

In the first coding example, we are going to check whether a number is prime or not. We first input the number using Buffered Reader Stream input. Then we have a for loop in which we are going to check the divisibility of the number by any other number except for 1 and any other number. The for loop starts from 2, and then the loop goes on till half the respective number. Then we have a variable that reports if the number is divisible or not by any number, which is a part of them loop. The code and the part of the loop are shown below, which inputs a number and gives the respective output whether that number is a prime number or not. In the program, the java.io.* is imported so that there are input/output operations appearing in the following code lines. Also, if there is an IOException, then it is treated differently. We have given the throws IO Exception command after the main’s declaration, which throws an exception that occurs during the input/output operation. Other than that, there are meaningful names used in the program for anyone reading the program to understand.

**Code:**

`import java.io.*;`

public class Prime

{

public static void main(String[] args) throws IOException

{

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

System.out.println("ENTER A NUMBER TO CHECK IF IT IS PRIME OR NOT");

int num= Integer.parseInt(br.readLine());

boolean count = false;

for(int i = 2; i <= num/2; ++i)

{

// condition for nonprime number

if(num % i == 0)

{

count = true;

break;

}

}

if (!count)

System.out.println(num + " is a prime number.");

else

System.out.println(num + " is not a prime number.");

}

}

**Output:**

**Code Explanation:** In the output, we see whether an inputted number is prime or not. First, we enter 29 as a number to check whether it is prime or not. We find out that 29 is a prime number as it is divisible by only the number of unity and the number itself. Except for that, it is not divisible by any other number.

Second, we enter another number to check whether the number is prime or not. We enter 58 as the number, and then we check whether the number is prime or not. Finally, we find out that 58 is not a prime number, but it is a composite number. It is an even number that is divisible by 2, 29 other than unity and the number itself.

4.8 (11,489 ratings)

View Course

#### Example #2 – Using While Loop

In the coding example, we are going to see the usage of the While loop for checking whether a number is prime or not. We use the same logic as the for loop, but we have a different outlook on the program.

**Code:**

`import java.io.*;`

public class PrimeNumber

{

public static void main(String[] args)throws IOException

{

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

System.out.println("ENTER A NUMBER TO CHECK IF IT IS PRIME OR NOT");

int num= Integer.parseInt(br.readLine());

int i = 2;

boolean count = false;

while(i <= num/2)

{

// condition for nonprime number

if(num % i == 0)

{

count = true;

break;

}

++i;

}

if (!count)

System.out.println(num + " is a prime number.");

else

System.out.println(num + " is not a prime number.");

}

}

**Output:**

**Code Explanation:** In the sample output, we input two odd numbers to check whether the numbers are prime or not. We enter 71 and 37 as two numbers and then finally find out that both the numbers are prime numbers as they are divisible by only 1 and the number itself.

#### Example #3 – Using Count

In this coding example, we are going to check the prime numbers within a range of numbers. We enter a minimum number as 20 and the maximum number as 50, and then we find the prime numbers within that range. It is a very easy program, and just by changing the min and max variables, we can find the prime numbers between the min and the max variables. The coding example is shown below.

**Code:**

`import java.io.*;`

public class PrimeRange

{

public static void main(String[] args)

{

int min = 20, max = 50;

while (min < max) {

boolean count = false;

for(int i = 2; i <= min/2; ++i) {

// condition for nonprime number

if(min % i == 0) {

count = true;

break;

}

}

if (!count)

System.out.print(min + " ");

++min;

}

}

}

**Output:**

**Code Explanation:** In the above code, we find the number of prime numbers between 20 and 50. We find the numbers which are divisible only by unity and the number itself. The numbers which are prime are 23, 29, 31, 37, 41, 43, and 47. The code uses a single for loop, which is used to check the number’s divisibility by the respective for loop control variable. If the number is divisible, it is a composite number, and if the number is not divisible, it is a prime number.

### Conclusion

In this article, we see the prime numbers working using a for loop and a while loop. Also, we see the prime numbers which are present within a certain range. The logic used for checking a prime number using a for loop and a while loop is almost the same. So, the checking of prime numbers is quite easy. The loop control variable is a very important factor in checking whether a number is prime or not.

### Recommended Articles

This is a guide to Prime Numbers in Java. Here we discuss the introduction and different examples of prime numbers in java along with code implementation. You may also look at the following articles to learn more-