Related Tags

java
communitycreator

# How to check if a number is prime in Java

Sumit Sharma

In this shot, we will discuss how to check whether a given number is prime or not in Java.

Before that, let’s go over what a prime number is.

All numbers that are greater than one and have only two factors, i.e., one and the number itself, are identified as prime numbers. We can also understand a prime number as a number that has exactly two divisors.

For example, let’s take the numbers two through seven:

• Divisors of 2 are 1 and 2

• Divisors of 3 are 1 and 3

• Divisors of 4 are 1, 2, and 4

• Divisors of 5 are 1 and 5

• Divisors of 6 are 1, 2, 3, and 6

• Divisors of 7 are 1 and 7

Two, three, five, and seven are the numbers that have exactly two divisors, i.e., one and the respective number itself, so they are prime numbers. Four and six are non-prime numbers/composite numbers.

### The isPrime() function

We can use the isPrime() function, which takes the number as input and returns true if the number is prime and false if it is not.

### How to find a prime number in Java

• Take a number as input.
• Check if there is a divisor of the number in the range of [two, number] because two is the smallest prime number.
• There is no number that can be completely divided by more than half of the number itself. We need to loop through two to the number itself divided by two (number/2).
• If any divisor is found, then the number is not prime; otherwise, the given number is prime.

### Code

Let’s look at an example that takes a number as input and returns a string that states whether the number is a prime number or a non-prime number. Enter a number and then press the ‘RUN’ button.

import java.util.Scanner;

class Prime {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number to check if it is truly prime number or not: ");
int number= sc.nextInt();
if(isPrime(number)) {
System.out.println(number + " is prime number");
}
else{
System.out.println(number + " is a non-prime number");
}
}
static  boolean isPrime(int num)
{
if(num<=1)
{
return false;
}
for(int i=2;i<num/2;i++)
{
if((num%i)==0)
return  false;
}
return true;
}

}


Enter the input below

### Explanation

Enter a number in the input section.

• In line 1, we import the java.util.Scanner library to read input from the user.

• In line 7, we take the input from the user and store it in a variable of int type number using the Scanner class of Java.

• In line 8, we call the isPrime() function and pass the taken number as a parameter. This function returns true if the number is prime; otherwise, it returns false. The output will be printed based on what the function returns.

• In lines 15 to 27, we define the isPrime() function. Inside isPrime(), we first check if the number is less than or equal to one, and if the condition is satisfied, then it simply returns false.

• In lines 21 to 25, we run a for loop from 2 to number/2, and if any divisor of the number is found, then it returns false (the number is not prime).

• In line 26, after we end the loop, it returns true because we do not find any divisor of the number.

This way, we can check whether the given number is prime or not in Java.

RELATED TAGS

java
communitycreator

CONTRIBUTOR

Sumit Sharma
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time 