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 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 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 sqrt.
Both are perfect squares.
Verify float sqrt by squaring.
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 -> not a square
0 -> 0 * 0 = 0 perfect
1 -> 1 * 1 = 1 perfect */ | Item | Type | Description |
|---|---|---|
n / number | int | Value to test (non-negative for yes). |
| Return | int (0/1) | 1 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 |
| sqrt | root = (int)sqrt(n); root*root == n | Fast and exact for integers |
| float sqrt | round(sqrt(n))**2 == n | Risky for large n — avoid |
| Goal | Pattern |
|---|---|
| Reject negatives | if (n < 0) return 0; |
| Loop check | while i * i <= n: |
| Exact hit | if (i * i == n) return 1; |
| sqrt + verify | root = 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 yes/no 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 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 (no math.h).
#include <stdio.h>
int isPerfectSquare(int number) {
int i;
for (i = 1; i * i <= number; i++) {
if (i * i == number) {
return 1;
}
}
return 0;
}
int main(void) {
int testNumber = 16;
if (isPerfectSquare(testNumber)) {
printf("%d is a perfect square.\n", testNumber);
} else {
printf("%d is not a perfect square.\n", testNumber);
}
return 0;
} Candidates advance from 1 while i * i has not passed 16. When i reaches 4, the product matches and the function returns 1. (Handle 0 separately if you need it — this loop starts at 1.)
Use sqrt from <math.h>, then verify by squaring.
Use sqrt and print all perfect squares from 1 to 50. Link with -lm on GCC/Clang.
#include <stdio.h>
#include <math.h>
int isPerfectSquare(int num) {
int root = (int)sqrt((double)num);
return root * root == num;
}
int main(void) {
printf("Perfect Squares in the Range 1 to 50:\n");
for (int i = 1; i <= 50; ++i) {
if (isPerfectSquare(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
} sqrt returns a floating root; casting to int truncates toward zero. Squaring that root recovers num exactly when num is a perfect square.
Build squares directly instead of filtering every integer.
#include <stdio.h>
int main(void) {
printf("First squares from k = 0 to 7:\n");
for (int k = 0; k <= 7; ++k) {
int square = k * k;
printf("%d * %d = %d\n", k, k, square);
}
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 call sqrt(n) and cast.
Exact match means perfect square.
1 with root k, or 0.
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 1 |
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.
Always square the truncated root again.
Loop for clarity; sqrt for a short check.
k*k builds the sequence without scanning.
Pro Tip: lead with the loop in interviews, then mention sqrt as the robust alternative.
Small habits that keep square checks interview-ready.
Return 0 immediately for n < 0.
Cast sqrt to int, then check root * root == n.
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 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 1 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 0 for negatives in this tutorial.
Prefer sqrt over float sqrt.
Between 9 and 16 — not square.
4 * 4 = 16.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
sqrt.(int)sqrt(n) by squaring. The loop takes O(sqrt(n)) checks. Return 0 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) |
| 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 sqrt, reject negatives, and remember that 0 and 1 count.
Practice the three examples above, then continue to finding the average of N numbers.
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 is also a quadratic residue in everyday arithmetic: the count of objects you can arrange in a square grid with the same number of rows and columns. The gaps between consecutive squares 1, 4, 9, 16… grow by the odd numbers 3, 5, 7, 9…
Learn how to find the average of N numbers in C.
9 people found this page helpful