Prime number is that number which is greater than 1 and only divisible by 1 and itself i.e the number should not be divisible by any number between 1 and itself.

Example 5, 7, 11, 17 etc.

Prime numbers from 1 to 100

So we need to write a Java Program to check whether the given number is Prime or not. To do that we need to follow the following steps.

Steps to Check Prime Number in Java

  1. Take a number as input (num).
  2. Run a for loop from 2 to (num-1).
  3. check if (num) is divisible by any of the loop numbers.
  4. If the number is divisible, then it is not Prime else its a Prime number.

Java Program to Check Prime Number

We have used the Boolean flag variable to indicate whether the number is prime or not. If its value remains true then only the number is ensured to be prime.


Java Program to Check Prime Number using Function

Here we will use the same logic mentioned above only we need to shift the for loop which checks the divisibility factor of the number into another function and that function will return true or false depending on whether the passed number is Prime or not.


Java Program to Print Prime Numbers From 1 to 100

In this program we need to individually pass all numbers from 1 to hundred to isPrime() method using for loop to check whether they are prime or not.


If you have any problem understanding the program then comment below.

Leave a Reply

Close Menu