How to check the given number is prime number or not in java.

Prime numbers are those number which are not divisible by any other number except itself or one.

To check the given number is prime or not you have to first check the number by dividing it from number 2 to n-1
if it is divided by any number except itself than it is not a prime number else it is prime number.

Java code for this program:-

           import java.util.Scanner;
          public class prime_number {
         public static void main(String[] args) {
        Scanner s=new Scanner(System .in);
        int n,lim;
      lim=(int) Math.sqrt(n);
     for(int x=2;x<=lim;x++)
              System.out.println(“The number is not a prime number”);

     System.out.println(“The number is a prime number”);


Output :-

Leave a Reply