prime number program in java

In this post, we will see about prime number program in java.
The prime number is the number which is divisible only by two numbers, 1 and itself.

Let’s write simple java program.


19 is prime number:true
893 is prime number:false

If you notice here, we have for loop upto num/2 but you can simply check upto square root of number and it will work fine.You don’t need to check upto num/2.
Let’s change the program and loop upto square root of num.


23 is prime number: true
449 is prime number: false

That’s all about prime number program in java.

Leave a Reply

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