Misc-Java-Examples

How to check if a number is a prime number

Posted On
Posted By admin

In this blog post, I will be demonstrating how you can check if a number is a prime number. Consider the following code snippet:


public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.println("Input the number to be checked:");

int num=scanner.nextInt();

boolean isPrime = true;

if (num == 0 || num == 1) {
isPrime = false;
}
else{

for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}


if(isPrime)
System.out.println("The number "+num+" is prime!");
else
System.out.println("The number "+num+" is not prime!");

}


 

The code first accepts a number from the user. It then checks if the number is 0 or 1, both of which are not prime numbers. It then iterates from 2 till the number/2. It checks if the remainder obtained after dividing the input number with the current index of the for loop is 0. If so, the number is not prime and so it exits the for loop.

So if you run the code for the value 13, the following output will be printed:

Input the number to be checked:
13
The number 13 is prime!

Similarly, if you run the code for the value 9, the following output will be printed:

Input the number to be checked:
9
The number 9 is not prime!
If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Java course here

Also, if you'd like to test your Java knowledge, do check out my practice tests course here


If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment