Check Perfect Square in C#
What you’ll learn
- What a perfect square is:
n = k × kfor some integerk. - Two C# approaches: a loop that squares integers up to
√n, and aMath.Sqrtcheck. - How to list perfect squares from
1to50and use the live preview widget.
What is a perfect square?
A perfect square is an integer that equals some whole number multiplied by itself. Examples: 1, 4, 9, 16, 25. Values like 2, 3, 5, 15 are not perfect squares.
Loop method
Try i * i for i = 0, 1, 2… until i * i > n.
Sqrt method
(int)Math.Sqrt(n) squared equals n when it is a square.
Range listing
1..50 yields 1 4 9 16 25 36 49.
Prerequisites
Integer multiplication, loops, and basic Math.Sqrt.
- You understand squaring:
4 × 4 = 16. - You can write a
forloop and cast adoubletoint.
The idea
Ask whether any integer k satisfies k * k == n. If yes, n is a perfect square. For 15, the nearest squares are 9 = 3² and 16 = 4², so it fails.
Intuition examples
16 = 4 × 4 (perfect). 15 sits strictly between 3² and 4² (not perfect). 0 and 1 are perfect squares too.
Live preview
Enter a nonnegative integer and see the integer square root plus the verdict.
Algorithm
Reject negatives
If n < 0, return false.
Find candidate root
Either loop i while i * i <= n, or compute root = (int)Math.Sqrt(n).
Verify
Return true when root * root == n (or loop finds matching i * i).
📜 Pseudocode
function isPerfectSquare(n):
if n < 0:
return false
for i from 0 while i * i <= n:
if i * i == n:
return true
return false Check one number (loop method)
Reference approach: try squaring each integer starting at 0. Tests 16.
using System;
class Program
{
static bool IsPerfectSquare(int number)
{
if (number < 0)
return false;
for (int i = 0; i * i <= number; i++)
{
if (i * i == number)
return true;
}
return false;
}
static void Main()
{
int testNumber = 16;
if (IsPerfectSquare(testNumber))
Console.WriteLine($"{testNumber} is a perfect square.");
else
Console.WriteLine($"{testNumber} is not a perfect square.");
}
} Perfect squares from 1 to 50
Uses Math.Sqrt for a compact check inside a range loop — matches the reference range program.
using System;
class Program
{
static bool IsPerfectSquare(int num)
{
if (num < 0)
return false;
int sqrt = (int)Math.Sqrt(num);
return sqrt * sqrt == num;
}
static void Main()
{
Console.WriteLine("Perfect squares in the range 1 to 50:");
for (int i = 1; i <= 50; i++)
{
if (IsPerfectSquare(i))
Console.Write($"{i} ");
}
Console.WriteLine();
}
} Optimization notes
Prefer sqrt for speed. (int)Math.Sqrt(n) then square-check is the usual interview answer for single values.
Binary search root. For very large integers, binary search the root in O(log n) without floating point.
❓ FAQ
🔄 Input / output examples
n | Integer root | Result |
|---|---|---|
16 | 4 | Perfect square |
15 | 3 (3²=9) | Not a perfect square |
1 | 1 | Perfect square |
0 | 0 | Perfect square |
Edge cases and pitfalls
Both are squares
Start the loop at 0 or handle n == 0 explicitly.
Return false early
No integer k satisfies k * k = n when n < 0.
Re-square the root
Never trust Math.Sqrt(n) == Math.Floor(Math.Sqrt(n)) alone on doubles — multiply back.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
Loop up to √n | O(√n) | O(1) |
Math.Sqrt check | O(1) for typical int | O(1) |
Range 1..R | O(R × cost per check) | O(1) |
Summary
- A perfect square equals
k × kfor some integerk. - Loop or
Math.Sqrtboth work — guard negatives and verify by squaring the root. - In
1..50, only seven values are perfect squares.
Perfect squares are 0, 1, 4, 9, 16, 25, 36… The gaps between consecutive squares are odd numbers: 1, 3, 5, 7, 9…
8 people found this page helpful
