Definition
n = k²
Some integer k squares to n.
A perfect square equals k * k for some whole number k. Examples: 1, 4, 9, 16, 25. This tutorial covers the loop and std::sqrt approaches, a live checker, worked C++ examples, edge cases, and complexity.
n = k²
Some integer k squares to n.
Beginner
Try candidates while i*i <= n.
Robust
Integer root, then root*root == n.
Both square
0*0 and 1*1 both count.
Try 16 / 15
See k and the verdict instantly.
Different idea
Squares vs divisor sums.
A perfect square is a non-negative integer that equals some integer squared. So 16 is perfect because 4 * 4 = 16, while 15 is not because no whole k works.
Interviews usually accept either a clear i * i loop or a std::sqrt check. Prefer integer roots over floating sqrt so large values stay exact.
It is a classic math interview warm-up that teaches exact integer reasoning without float traps.
Some integer k squares to n.
Loop or std::sqrt.
Both are perfect squares.
verify after std::sqrt.
In short: find whether some integer k satisfies k * k == n.
Given an integer n, decide whether it is a perfect square of a non-negative integer.
// 16 -> 4 * 4 = 16 perfect
// 15 -> no integer k not perfect
// 0 -> 0 * 0 = 0 perfect
// 1 -> 1 * 1 = 1 perfect | Item | Type | Description |
|---|---|---|
n / number | int | Value to test (non-negative for yes). |
| Return | bool | true when some k has k*k == n. |
| Optional k | int | The integer root when the answer is yes. |
function is_perfect_square_loop(n):
if n < 0:
return false
i = 0
while i * i <= n:
if i * i == n:
return true
i = i + 1
return false | Method | Idea | Notes |
|---|---|---|
| i*i loop | Try candidates until square exceeds n | Clearest for beginners |
| std::sqrt | root = static_cast<int>(std::sqrt(n)); root*root == n | Fast and exact for integers |
| float sqrt | std::lround(std::sqrt(n))² == n | Risky for large n — avoid |
| Goal | Pattern |
|---|---|
| Reject negatives | if (n < 0) return false; |
| Loop check | while (i * i <= n) |
| Exact hit | if (i * i == n) return true; |
| sqrt check | int root = static_cast<int>(std::sqrt(n)); |
| Verify root | return root * root == n; |
| Build squares | k * k for k = 0, 1, 2, … |
Same question — different reliability.
while i*i <= nInterview-friendly and exact
root*root == nPreferred production check
avoid for intsRounding can lie on big n
k*k vs s(n)=nDifferent “perfect” meaning
Reach for a square check whenever you need exact integer roots.
Simple math with an exactness twist.
Can n form a square layout?
Keep only square values in a range.
Show why integer roots beat floats.
This tutorial targets integer n.
Key benefit: one crisp bool question that forces you to think in exact integers, not approximate roots.
Checks with integer logic, then reports the root and verdict.
Three complete C++ programs — loop check for 16, list squares from 1 to 50 with std::sqrt, and generate squares by squaring. Click View Output to reveal sample console results.
A beginner-friendly loop that never needs floating roots.
Simple and beginner-friendly perfect square check.
#include <iostream>
bool isPerfectSquare(int number) {
if (number < 0) {
return false;
}
int i = 0;
while ((long long) i * i <= number) {
if (i * i == number) {
return true;
}
i++;
}
return false;
}
int main() {
int testNumber = 16;
if (isPerfectSquare(testNumber)) {
std::cout << testNumber << " is a perfect square.\n";
} else {
std::cout << testNumber << " is not a perfect square.\n";
}
return 0;
} Candidates advance from 0 while i * i has not passed 16. When i reaches 4, the product matches and the function returns true.
Use the standard library for a crisp, exact check.
Use std::sqrt and print all perfect squares from 1 to 50.
#include <iostream>
#include <cmath>
bool isPerfectSquare(int num) {
if (num < 0) {
return false;
}
int root = static_cast<int>(std::sqrt(num));
return root * root == num;
}
int main() {
std::cout << "Perfect Squares in the Range 1 to 50:\n";
for (int i = 1; i <= 50; i++) {
if (isPerfectSquare(i)) {
std::cout << i << " ";
}
}
std::cout << "\n";
return 0;
} Casting std::sqrt(num) to int gives a candidate root. Squaring that root recovers num exactly when num is a perfect square.
Build squares directly instead of filtering every integer.
#include <iostream>
int main() {
std::cout << "First squares from k = 0 to 7:\n";
for (int k = 0; k <= 7; k++) {
int square = k * k;
std::cout << k << " * " << k << " = " << square << "\n";
}
return 0;
} When you only need the square sequence, squaring consecutive integers is cheaper than testing every n in a range.
No non-negative integer squares to a negative.
Loop i while i*i <= n, or cast std::sqrt(n).
Exact match means perfect square.
true with root k, or false.
Trace the loop method for n = 16.
| i | i * i | i*i <= 16? | Match? |
|---|---|---|---|
0 | 0 | Yes | No |
1 | 1 | Yes | No |
2 | 4 | Yes | No |
3 | 9 | Yes | No |
4 | 16 | Yes | Yes — return true |
4 * 4 equals 16 — perfect square.
Where perfect-square checks show up beyond the interview prompt.
Exact integer math checks.
Example: is_square(16).
List squares inside a band.
Example: 1..50 list.
Build squares with k*k.
Example: Example 3.
Can n tiles form a square?
Example: 25 -> 5×5.
Show exact integer roots.
Example: avoid float sqrt.
Continue the interview chain.
Example: related CTA.
Pro Tip: say “I’ll check whether root*root equals n using an integer root” before coding.
Why these approaches work well for beginners and interviews.
Dry-run 16 on paper and watch i grow.
No float rounding surprises with std::sqrt.
Loop for clarity; std::sqrt for speed.
k*k builds the sequence without scanning.
Pro Tip: lead with the loop in interviews, then mention std::sqrt as the robust alternative.
Small habits that keep square checks interview-ready.
Return false immediately for n < 0.
Exact integer root for production code.
Never trust a root without squaring back.
Use k*k if you need the sequence itself.
Name the definition so interviewers know you know.
Pro Tip: sanity-check 0, 1, 16, and 15 — if those four behave, your logic is solid.
Mistakes that commonly break perfect-square programs.
Large ints can round incorrectly.
→ Use std::sqrt or an integer loop.
Taking floor(sqrt) without squaring back.
→ Always compare root * root to n.
Different “perfect” concept entirely.
→ This page is about k * k.
Starting i at 1 and rejecting zero.
→ 0 = 0 * 0 is a square.
Returning true for -16 in real-integer checks.
→ Reject n < 0 in this tutorial.
Handle these before claiming the check is complete.
0 = 0 * 0.
1 = 1 * 1.
Return false for negatives in this tutorial.
Prefer std::sqrt from <cmath>.
Between 9 and 16 — not square.
4 * 4 = 16.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
std::sqrt.std::sqrt from <cmath>. The loop approach takes O(sqrt(n)) checks. Return false immediately for negatives.Quick Takeaway: n is a perfect square when some integer k satisfies k * k == n.
| Approach | Time (single n) | Extra space |
|---|---|---|
| Loop until i*i > n | O(sqrt(n)) | O(1) |
| std::sqrt-based check | O(1) practical | O(1) |
| Range 1..U scan | O(U) checks | O(1) |
For interview demos, either method is fine; mention float pitfalls when asked about reliability.
A perfect square equals some integer squared. Use an i * i loop or std::sqrt, reject negatives, and remember that 0 and 1 count.
Practice the three examples above, then continue to checking powers of 2.
n = k² means perfect square; verify with integers, not float sqrt.
Decide exact squares the interview-friendly way.
n = k * k
Definitionwhile i*i
Methodroot*root
Robust0, 1 yes
GuardsO(√n)
AnalysisA perfect square can be arranged into a square grid with equal rows and columns. The gaps between consecutive squares (1, 4, 9, 16...) are odd numbers (3, 5, 7, 9...).
Learn how to find the average of N numbers in C++.
9 people found this page helpful