Square Number Pyramid in C#

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + Math

What You’ll Learn

How to print a centered pyramid of square numbers in C# using nested loops and formatted output.

We increment a counter m and print m*m with a fixed width so the pyramid stays aligned.

⭐ Pattern Output

For this example, the pattern looks like this:

Output
        1
    4   9  16
25  36  49  64  81
100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625
1

Complete C# Program

Print odd-length rows (1, 3, 5, 7, 9). Each printed value is the square of the running counter.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, k;
            int m = 1;

            for (i = 1; i <= 9; i += 2)
            {
                for (j = i; j < 9; j++)
                    Console.Write("  ");

                for (k = 1; k <= i; k++)
                {
                    Console.Write("{0,4}", m * m);
                    m++;
                }

                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Use odd row sizes

i takes values 1, 3, 5, 7, 9, which become the count of squares printed on each row.

Row sizing
2

Indentation for centering

The loop for (j = i; j < 9; j++) prints spaces so smaller rows start farther right.

Alignment
3

Print squares using m*m

Each iteration prints m*m and increments m.

Squares
4

Fixed-width formatting

{0,4} keeps columns aligned as the squares grow (e.g., 1, 16, 625).

Formatting
2

Variation — User Input Levels

This version lets the user choose how many odd-length rows to print (levels).

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int levels;
            int m = 1;

            Console.Write("Enter number of levels: ");
            if (!int.TryParse(Console.ReadLine(), out levels) || levels <= 0)
            {
                Console.WriteLine("Please enter a positive integer.");
                return;
            }

            int maxWidth = 2 * levels - 1;

            for (int i = 1; i <= maxWidth; i += 2)
            {
                for (int j = i; j < maxWidth; j++)
                    Console.Write("  ");

                for (int k = 1; k <= i; k++)
                {
                    Console.Write("{0,4}", m * m);
                    m++;
                }

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Use long if you plan to print many squares (values grow quickly)
  • Change m*m to m*m*m to print cubes instead of squares
  • Increase the indentation spacing if you add spaces between numbers

Avoid

  • Using too small a field width (alignment will break for 3+ digit squares)
  • Letting the counter overflow for large levels without switching types

Key Takeaways

1

Row lengths are odd: 1, 3, 5, 7, 9.

2

A counter generates the sequence; printing m*m gives squares.

3

Indentation centers the pyramid.

4

Formatted printing keeps columns aligned.

Explore More C# Number Patterns!

Square-number patterns combine loops with simple math—great practice for both.

All Number Patterns →
Did you know?

The squares grow quickly: \(25^2 = 625\) already needs 3 digits. Fixed-width formatting keeps the pyramid aligned even as values increase.

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.

12 people found this page helpful