C++ 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 C++.


64 => 4 : not prime

49 => 7 : number is Prime


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

Run it Now
#include<iostream>
#include<math.h>
using namespace std;

int main() {

    int x, y = 1, z, i;

    cout << "Enter a Number :";
    cin>>x;

    z = sqrt(x);
    cout << "\nNumber Square Root is :" << z;

    if (z == 1) {
        cout << "\nThe Number is Not Prime";
    } else {
        for (i = 2; i < z; i++) {
            if (z % i == 0) {
                y = 0;
                break;
            }
        }
        if (y == 1) {
            cout << "\nNumber is Prime :" << z;
        } else {
            cout << "\nNumber is Not Prime :" << z;
        }
    }

    return 0;
}

Output:

Enter a Number :16
Number Square Root is :4
Number is Not Prime :4