Find 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)

Example

#include<iostream>
#include<cmath>
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);
}

Output

Nearest perfect square: 1024
Updated on: 2019-11-04T07:55:45+05:30

485 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements