Java Program to check square root of a number is prime or not


In this program, You will learn how to check square root of a number is prime or not in Java.


Some list of prime numbers is : 2, 3, 5, 7, 11


Example: How to check square root of a number is prime or not in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int n, i, j, p = 1, z;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a Number :");
        n = sc.nextInt();

        z = (int) Math.sqrt(n);
        System.out.print("\nThe Square Root is :" + z);

        for (i = 2; i < z; i++) {

            if (z % i == 0) {
                p = 0;
                break;
            }
        }
        if (p == 1){
          System.out.println("\nThe Numbers is Prime :" + z);
        }else{
          System.out.println("\nThe Numbers is Not Prime :" + z);
        }

    }
}

Output:

Enter a Number :64
The Square Root is :8
The Numbers is Not Prime :8