C# Number Pattern (Decreasing then Increasing)

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

What Is This Pattern?

A mixed number triangle builds each row from two halves: descending i..2, then ascending 1..(rows - i + 1).

Remember
Rule: descend i..2, then ascend 1..(rows-i+1)

12345
21234
32123
43212
54321   ← rows = 5

In C# use one outer loop and two inner loops. Every row prints exactly rows digits — left half shrinks as the right half grows.

How to Solve It

One outer row loop, then descending and ascending half-loops.

MethodIdeaBest for
Two-half rowDescend i..2, then ascend 1..(rows-i+1)Learning, interviews, exams
Rows inputSame logic with a user-chosen widthPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i down while j > 1:
        print j
    for k from 1 to (rows - i + 1):
        print k
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (i = 1; i <= rows; i++)
Descending halffor (j = i; j > 1; j--) Console.Write(j);
Ascending halffor (k = 1; k <= rows + 1 - i; k++) Console.Write(k);
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit in both halves
Console.WriteLineEnds the current lineAfter both half-loops finish a row

Live Preview

Change the row count and the mixed triangle updates instantly.

Whole numbers from 1 to 9 (keeps single-digit rows). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 25 digits
12345
21234
32123
43212
54321

Worked Walkthrough — rows = 4

Trace descending and ascending halves on each row.

iDescend / ascendPrinted row
1(none) / 12341234
22 / 1232123
332 / 123212
4432 / 14321

Each row has rows digits. Total prints for n rows = n².

C# Programs

Three complete programs: fixed 5 rows, user-input rows, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

Two inner loops: descend i..2, then ascend 1..(rows-i+1).

C#
using System;

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

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j > 1; j--)
                    Console.Write(j);

                for (k = 1; k <= rows + 1 - i; k++)
                    Console.Write(k);

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i picks the row — larger i means a longer descending half.

2. Descend. Print j from i down while j > 1 (skipped when i = 1).

3. Ascend. Print k from 1 to rows + 1 - i so the row stays width rows.

Example 2 — User Input (rows)

Read the row count and build the same mixed triangle.

C#
using System;

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

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

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j > 1; j--)
                    Console.Write(j);

                for (k = 1; k <= rows + 1 - i; k++)
                    Console.Write(k);

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

2. Same two halves. Only the width changes from the literal 5.

Example 3 — Compact rows = 3

A smaller fixed demo — easier to trace by hand.

C#
using System;

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

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j > 1; j--)
                    Console.Write(j);

                for (k = 1; k <= rows + 1 - i; k++)
                    Console.Write(k);

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same rules. Descend then ascend; each row stays three digits wide.

2. Quick check. Row 1 is all ascending; row 3 is almost all descending ending in 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

j >= 1

Descend to 1 inclusive

That doubles the digit 1 with the ascending half. Keep j > 1 (or j >= 2).

wrong bound

Ascending to rows every time

Rows get too long. Use rows + 1 - i so width stays constant.

WriteLine

WriteLine inside a half-loop

That breaks the row. Call WriteLine only after both inner loops.

rows = 1

Single row

Output is just 1 — descending is skipped.

spaces

Add spaces between digits

Fine for readability — use Console.Write(j + " ") — but the classic pattern is tight digits.

Bad input

Convert.ToInt32 throws

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

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Each of n rows prints exactly n digits, so total work is n².

Key Takeaways

  • Rule: descend i..2, then ascend 1..(rows-i+1).
  • Width: every row has exactly rows digits — left grows as right shrinks.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after both halves.
  • Next step: Program 51 alternates ascending and descending rows with a running counter.

One line: for each row, print a descending prefix then an ascending suffix that keep the width fixed.

Frequently Asked Questions

Each row prints two parts: descending from i down to 2, then ascending from 1 up to (rows - i + 1). Row 2 becomes 2 + 1234 = 21234.
One prints the descending half (j = i down while j > 1). The other prints the ascending half (k = 1..(rows-i+1)). Splitting them keeps both halves clear.
When i = 1, the descending loop never runs. Only the ascending loop prints 1..rows — a full ascending line.
The descending loop prints i first. For i = 2 that is 2, then the ascending loop prints 1..4.
Change rows or read it from user input with TryParse — see Example 2.
O(n²) for n rows because each row prints n digits — total prints = n².
Program 49 prints i*j products on each row. Program 50 concatenates digit sequences — descending then ascending — with no multiplication.
Both work for the descending loop and print the same i..2 sequence.
One row prints 1 — the descending loop is skipped and the ascending loop prints only 1.

Did you know?

Each row combines two sequences: descending i..2, then ascending 1..(rows-i+1). Row 2 prints 21234; row 5 prints 54321. Each row has rows digits — total prints = n².

Next: Alternating Ascending/Descending Triangle

Print continuous numbers that flip direction on odd and even rows.

Program 51 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

6 people found this page helpful