Java Program to Check Whether Number Prime or Not

This post shows a Java program to check whether the passed number is a prime number or not.

A number is a prime number if can be divided either by 1 or by the number itself. So the logic for your program should be to run a for loop and divide the passed number every time in that loop, if it completely divides any time then the passed number is not a prime number. You only need to run your loop from 2 till N/2 (where N is the passed number), reason being no number is completely divisible by a number more than its half.

Java program to check whether the number is prime or not

Output

That’s all for the topic Java Program to Check Whether Number is Prime or Not. If something is missing or you have something to share about the topic please write a comment.


You may also like

One Comment

  1. Thanks for tnis interesting post, I hzve shared it on Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.