Check Perfect Square in C#

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Square-root check

What you’ll learn

  • What a perfect square is: n = k × k for some integer k.
  • Two C# approaches: a loop that squares integers up to √n, and a Math.Sqrt check.
  • How to list perfect squares from 1 to 50 and 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 for loop and cast a double to int.

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 and (not perfect). 0 and 1 are perfect squares too.

Live preview

Enter a nonnegative integer and see the integer square root plus the verdict.

Live result
Press “Run check”.

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

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
1

Check one number (loop method)

Reference approach: try squaring each integer starting at 0. Tests 16.

c#
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.");
    }
}
2

Perfect squares from 1 to 50

Uses Math.Sqrt for a compact check inside a range loop — matches the reference range program.

c#
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

A number n is a perfect square if there exists an integer k such that k * k = n — for example 16 = 4 * 4.
Yes. 0 = 0 * 0. The loop method should handle 0 explicitly or start from i = 0.
Yes. 1 = 1 * 1.
Not among integers in this tutorial. Return false for n < 0.
Math.Sqrt returns double; casting to int and squaring again avoids floating-point misclassification.
Sqrt check is O(1) per value for typical int sizes. The loop to sqrt(n) is O(sqrt(n)) and great for learning without library calls.

🔄 Input / output examples

nInteger rootResult
164Perfect square
153 (3²=9)Not a perfect square
11Perfect square
00Perfect square

Edge cases and pitfalls

Zero / one

Both are squares

Start the loop at 0 or handle n == 0 explicitly.

Negatives

Return false early

No integer k satisfies k * k = n when n < 0.

Floating point

Re-square the root

Never trust Math.Sqrt(n) == Math.Floor(Math.Sqrt(n)) alone on doubles — multiply back.

⏱️ Time and space complexity

MethodTimeExtra space
Loop up to √nO(√n)O(1)
Math.Sqrt checkO(1) for typical intO(1)
Range 1..RO(R × cost per check)O(1)

Summary

  • A perfect square equals k × k for some integer k.
  • Loop or Math.Sqrt both work — guard negatives and verify by squaring the root.
  • In 1..50, only seven values are perfect squares.
Did you know?

Perfect squares are 0, 1, 4, 9, 16, 25, 36… The gaps between consecutive squares are odd numbers: 1, 3, 5, 7, 9…

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful