C++ Determine Perfect Square Program
In this tutorial, we will demonstrate the logic of determine if the given number is a Perfect Square or not, in the C++ programming language.
sqrt(x) method returns the square root the
For a better understanding of its implementation, refer to the well-commented CPP code given below.
using namespace std;
// Returns true if the given number is a perfect square
bool isPerfectSquare(int n)
// sqrt() method is defined in the math.h library
// to return the square root of a given number
int sr = sqrt(n);
if (sr * sr == n)
cout << "\n\nWelcome to Studytonight :-)\n\n\n";
cout << " ===== Program to determine if the entered number is perfect square or not ===== \n\n";
// variable declaration
bool perfect = false;
// taking input from the command line (user)
cout << " Enter a positive integer : ";
cin >> n;
// Calling a method that returns true if the
// number is a perfect square
perfect = isPerfectSquare(n);
cout << "\n\nThe entered number " << n << " is a perfect square of the number " << sqrt(n);
cout << "\n\nThe entered number " << n << " is not a perfect square";
cout << "\n\n\n";
Let's see another number with number 81.
We hope that this post helped you develop a better understanding of the concept to check for perfect square, in C++. For any query, feel free to reach out to us via the comments section down below.
Keep Learning : )