C# Number Pyramid Pattern (Centered Continuous)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A centered continuous number pyramid prints odd-width rows of a running counter, padded with leading spaces so the numbers sit in a pyramid shape.

Remember
Rule: odd widths + spaces when j > i + k++

    1
  2 3 4
5 6 7 8 9   ← max = 5

In C# you use i += 2 for odd row widths, a reverse inner loop that prints a space when j > i (otherwise k++), then Console.WriteLine() ends each row. Keep k outside the outer loop so the sequence never restarts.

How to Solve It

One nested-loop idea with two useful variants — fixed odd max and force-odd input.

MethodIdeaBest for
Reverse inner + ifScan j = max..1; space when j > i, else k++Learning, interviews, exams
Force odd maxIf user enters even, subtract 1 before loopingSafer input demos

Pseudocode

Pseudocode
k = 1
for i from 1 to max step 2:
    for j from max down to 1:
        if j > i:
            print space
        else:
            print k, then k = k + 1
    print newline

Cheat sheet

GoalPattern
Odd row widthsfor (i = 1; i <= max; i += 2)
Scan columnsfor (j = max; j >= 1; j--)
Lead with spacesif (j > i) Console.Write(" ");
Print continuous numberselse Console.Write(k++ + " ");
Force odd maxif (max % 2 == 0) max -= 1;

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach space and each k++
Console.WriteLineEnds the current lineAfter the inner loop

Live Preview

Change the maximum odd width and the pyramid updates instantly — including the total number count.

Odd whole numbers from 1 to 9. Even values are adjusted down by 1. Tap a chip or type a value — the preview redraws as you go.

Live result max 5 · 9 numbers
    1 
  2 3 4 
5 6 7 8 9 

Worked Walkthrough — max = 5

Trace each odd row width i, how many leading spaces appear, and which k values print.

iSpaces (j > i)Printed numbersRow
1411
322 3 42 3 4
505 6 7 8 95 6 7 8 9

Total numbers are 1+3+5 = 9 — the sum of the odd widths up to max.

C# Programs

Three complete programs: fixed width, user-input width, and force-odd input. Use View Output for sample results.

Example 1 — Fixed max = 5

Odd widths with a reverse inner loop that centers a continuous counter.

C#
using System;

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

            for (i = 1; i <= 5; i += 2)
            {
                for (j = 5; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write(" ");
                    else
                        Console.Write(k++ + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Odd widths. i += 2 yields 1, 3, 5 as the number of values per row.

2. Center with spaces. When j > i, print a space; otherwise print k++.

3. Carry the counter. k is declared once, so row 2 continues at 2 and row 3 at 5.

Example 2 — User Input

Read the maximum odd width at runtime. Prefer int.TryParse so bad input does not throw.

C#
using System;

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

            Console.Write("Enter the maximum odd width: ");
            if (!int.TryParse(Console.ReadLine(), out max) || max < 1)
            {
                Console.WriteLine("Please enter a positive whole number.");
                return;
            }

            for (i = 1; i <= max; i += 2)
            {
                for (j = max; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write(" ");
                    else
                        Console.Write(k++ + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate max. TryParse rejects non-numeric input; require max >= 1.

2. Same nested loops. For max = 3 you get two rows: one centered 1, then 2 3 4.

Example 3 — Force Odd Max

If the user enters an even width, subtract 1 so every row length stays odd.

C#
using System;

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

            Console.Write("Enter the maximum width: ");
            if (!int.TryParse(Console.ReadLine(), out max) || max < 1)
            {
                Console.WriteLine("Please enter a positive whole number.");
                return;
            }

            if (max % 2 == 0) max -= 1;

            for (i = 1; i <= max; i += 2)
            {
                for (j = max; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write(" ");
                    else
                        Console.Write(k++ + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Normalize. Even input becomes max - 1 so the last row still has an odd count.

2. Same pyramid. Entering 6 behaves like Example 1 with max = 5.

Edge Cases & Pitfalls

Check these before calling the solution done.

reset k

k = 1 inside the outer loop

Every row restarts at 1. Keep k declared once before both loops.

i++

i++ instead of i += 2

That prints every width. Keep i += 2 for odd row sizes.

wrong compare

j >= i instead of j > i

You skip printing a number when j == i. Keep the strict greater-than check.

WriteLine early

WriteLine inside the inner loop

That puts each column on its own line. Call WriteLine() only after the row finishes.

even max

Last width would be even

Subtract 1 (Example 3) or reject even input so rows stay odd.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
Force odd (Example 3)O(n²)O(1)

There are (n+1)/2 rows; each scans n columns, so total work is O(n²).

Key Takeaways

  • Rule: odd widths via i += 2; space when j > i, else k++.
  • Carry over: declare k once so numbers continue across rows.
  • Write vs WriteLine: spaces and numbers stay on the line; WriteLine advances after each row.
  • Next step: Program 25 prints a bidirectional number triangle.

One line: for each odd width, print spaces then k++ across a reverse column scan, then WriteLine.

Frequently Asked Questions

The program prints leading spaces when j > i, which shifts the numbers to the right and centers the pyramid.
Because the counter k is not reset inside the outer loop. It increments with k++ each time a number is printed.
Row widths are odd (1, 3, 5, …) so each row adds two more numbers than the previous row.
Console.Write stays on the same line for spaces and numbers. Console.WriteLine ends the current row after the inner loop finishes.
The reverse inner loop lets you print leading spaces first (when j > i) and numbers afterward — a common centering trick.
Subtract 1 to force an odd width (see Example 3), or validate and prompt again.
O(n²) for max width n because each row iterates across n columns.
Prefer int.TryParse(Console.ReadLine(), out max) so bad input does not throw FormatException.
Only one row prints — a single 1 (with no leading spaces when max equals 1).

Did you know?

This centered pyramid prints numbers continuously using a counter k. An if inside a reverse loop prints leading spaces when j > i, then prints k++ once the column reaches the row boundary.

Next: Bidirectional Number Triangle

Print ascending then descending digits on each row for a left–right triangle.

Program 25 tutorial →

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