Mixed Number Triangle in C#

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

What You’ll Learn

How to print a mixed number triangle in C# where each row has a descending part followed by an ascending part.

For rows = 5, the output starts with 12345, then 21234, and ends with 54321. This is a neat way to practice splitting one row into two loop-driven sequences.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
12345
21234
32123
43212
54321
1

Complete C# Program

We use two inner loops: one prints i..2 (descending) and the other prints 1..(rows-i+1) (ascending).

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

Set the row count

int rows = 5; controls the height of the triangle.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) runs once per line and decides which row we are printing.

Row control
3

Two inner loops (descending + ascending)

First: for (j = i; j > 1; j--) prints i..2. Then: for (k = 1; k <= rows + 1 - i; k++) prints 1..(rows-i+1).

Number printing
4

Move to the next line

Console.WriteLine() starts the next row after printing both parts.

Line break
=

Mixed number triangle

Each row is a concatenation of two sequences. With n rows, total prints grow on the order of O(n²).

2

Variation — User Input Version

Let the user choose the number of rows using Console.ReadLine():

C#
using System;

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

            Console.Write("Enter the number of rows: ");
            rows = Convert.ToInt32(Console.ReadLine());

            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();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Validate input with int.TryParse before using rows
  • Add a separator like a space using Console.Write(j + " ")
  • Right-align the triangle by printing leading spaces before each row
  • Change the second part to count down to create different effects
  • Replace numbers with characters to create alphabet variants

Avoid

  • Forgetting Console.WriteLine() at the end of each row
  • Using zero/negative rows without validating input
  • Mixing row/column logic (keep the outer loop for rows)
  • Hardcoding values when you want a scalable pattern

Key Takeaways

1

Each row is built from two sequences: descending (i..2) then ascending (1..(rows-i+1)).

2

Using two inner loops keeps each part of the row simple and readable.

3

Changing rows automatically scales the pattern without rewriting the logic.

4

This technique also works for star patterns and alphabet patterns.

❓ Frequently Asked Questions

Because on row i = 2 we first print the descending part 2, then the ascending part 1234.
Yes. Set rows to a larger value (like 8). The loops adjust automatically: the first part becomes i..2 and the second part becomes 1..(rows-i+1).
Stopping at 2 avoids printing 1 twice because the ascending loop already prints 1 at the start of its sequence.
O(n²) for n rows, since the total number of printed digits grows proportionally to \(n^2\).

Explore More C# Number Patterns!

Practice nested loops with pyramids, triangles, and mixed patterns like this one.

All Number Patterns →
Did you know?

This output is built by concatenating two sequences per row. When you’re comfortable with this idea, you can create many new patterns by swapping the bounds (for example printing i..1 then 2.. something).

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