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

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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 04-Nov-2019 07:55:45

Advertisements