Mixed Number Triangle in C#

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:
12345
21234
32123
43212
54321Complete C# Program
We use two inner loops: one prints i..2 (descending) and the other prints 1..(rows-i+1) (ascending).
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
Set the row count
int rows = 5; controls the height of the triangle.
Outer loop (rows)
for (i = 1; i <= rows; i++) runs once per line and decides which row we are printing.
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).
Move to the next line
Console.WriteLine() starts the next row after printing both parts.
Mixed number triangle
Each row is a concatenation of two sequences. With n rows, total prints grow on the order of O(n²).
Variation — User Input Version
Let the user choose the number of rows using Console.ReadLine():
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.TryParsebefore usingrows - 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
Each row is built from two sequences: descending (i..2) then ascending (1..(rows-i+1)).
Using two inner loops keeps each part of the row simple and readable.
Changing rows automatically scales the pattern without rewriting the logic.
This technique also works for star patterns and alphabet patterns.
❓ Frequently Asked Questions
i = 2 we first print the descending part 2, then the ascending part 1234.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).1 at the start of its sequence.Explore More C# Number Patterns!
Practice nested loops with pyramids, triangles, and mixed patterns like this one.
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).
12 people found this page helpful
