Find the Next perfect square greater than a given number in C++

Suppose we have a number n. our task is to find next perfect square number of n. So if the number n = 1000, then the next perfect square number is 1024 = 322.

To solve this, we have get the square root of the given number n, then take the floor of it, after that display the square of the (floor value + 1)


using namespace std;
int justGreaterPerfectSq(int n) {
   int sq_root = sqrt(n);
   return (sq_root + 1)*(sq_root + 1);
int main() {
   int n = 1000;
   cout << "Nearest perfect square: " << justGreaterPerfectSq(n);


Nearest perfect square: 1024