# Prime Number in Java

Prime number is a number which is not divisible by another number except 1 and itself. 1 and less then 1 i.e., negative numbers are not prime numbers. Some example of Prime Numbers 2,3,5,7,11,13,17,19,23,29,31, etc.

## How to check Prime numbers in java

We can check if a given number is prime number or not by checking divisibility by numbers from 2 to half of the number. If any number in between 2 to half of number is able to divide the number completely ( remainder =0) then given number is not a Prime number. Else Number is a Prime Number

## Java Program to Check if Given Number is Prime Number

``````package com.TheCodeData;

import java.util.Scanner;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number ");
int number = sc.nextInt();
boolean prime = true;
if(number<2)
prime=false;
else
{
for(int i=2;i<=number/2;i++)
{
if(number%i==0)
{
prime=false;
break;
}
}
}

if(prime)
System.out.println("Given number is a Prime Number");
else
System.out.println("Given number is not a Prime Number");

}
}
``````

### Output

In the above java code for prime number at first we are taking input from user using Java Scanner Class. After that we are checking that if number is less then 2 if number is less then 2 then number is not a prime number because 1 and negative numbers are not prime number. After this we are running a loop from i=2 to i<= number/2 to check if any number can divide the number. after that we are printing the result.

## Prime Numbers FAQ

### Is 17 a prime number ?

Yes, 17 is a prime number since 17 is not divided evenly by any other number except 1 and 17, hence it is prime number.

### Is 29 a prime number ?

Yes, 29 is a prime number since 29 is not divided evenly by any other number except 1 and 29, hence it is a prime number.

### Is 1 a prime number ?

According to the definition of a prime number, a prime number is a natural number greater than 1 that has exactly two distinct positive divisors – 1 and itself. In the case of 1, it fails to meet this criteria as it has only one positive divisor which is 1. therefore 1, is not classified as a prime number.

### Is 13 a prime number ?

Yes, 13 is a prime number since 13 is not divided evenly by any other number except 1 and 13, hence it is a prime number.

### Is 11 a prime number ?

Yes, 11 is a prime number since 11 is not divided evenly by any other number except 1 and 11, hence it is a prime number.

### Is 9 a prime number ?

No, 9 is not a prime number. Number 9 is divisible by 1,3 & 9 i.e., it has divisors other than 1 and itself, hence it is not a prime number.

### Is 19 a prime number?

Yes, 19 is a prime number since 19 is not divided evenly by any other number except 1 and 19, hence it is a prime number.

### Is 23 a prime number?

Yes, 23 is a prime number since 23 is not divided evenly by any other number except 1 and 23, hence it is a prime number.

### Is 31 a prime number ?

Yes, 31 is a prime number since 31 is not divided evenly by any other number except 1 and 31, hence it is a prime number.

### Is 37 a prime number ?

Yes, 37 is a prime number since 37 is not divided evenly by any other number except 1 and 37, hence it is a prime number.

Note :- We can check all individual numbers , if they are prime number or not by using Java Code as we learn in this post.