Shape Rule
Right-aligned triangle
Row i prints numbers 1 to i, with leading spaces before the digits.

The right-aligned number triangle prints 1, then 1 2, then 1 2 3, … — a natural follow-up after Program 42’s hollow square border. This tutorial covers leading-space indentation, ascending sequences, fixed-width formatting, nested loops, a live preview, worked C# examples, edge cases, and complexity.
Right-aligned triangle
Row i prints numbers 1 to i, with leading spaces before the digits.
i = 1..rows
for (i = 1; i <= rows; i++) — ascending outer loop, one row per iteration.
rows..i+1
for (j = rows; j > i; j--) — prints a single space for right alignment.
{0,2} format
for (k = 1; k <= i; k++) then Console.Write("{0,2}", k).
3–7 rows
Pick a row count and draw the right-aligned number triangle in the browser.
Complexity
Total prints = n(n+1)/2 — work scales as n².
A right-aligned number triangle prints numbers from 1 to i on each row: 1, then 1 2, then 1 2 3, and so on. With rows = 5, shorter rows shift right thanks to a leading-space loop.
In C# you use three nested loops: print a space while j > i, then print Console.Write("{0,2}", k) for k = 1..i, then WriteLine().
It combines a space loop with an ascending number loop — a key step after Program 42’s hollow grid pattern.
Ascending sequence.
j > i spaces.
Fixed-width columns.
Follow Program 42; continue to Program 44 next.
In short: outer i = 1..rows, space loop j = rows..i+1, numbers k = 1..i with {0,2}, then WriteLine().
Given rows = 5, print a right-aligned ascending triangle: leading spaces while j > i, then numbers from 1 to i with fixed-width formatting.
// rows = 5
// 1
// 1 2
// 1 2 3
// 1 2 3 4
//1 2 3 4 5 | Item | Type | Description |
|---|---|---|
rows | int | Triangle height — also controls leading-space count. |
i | int | Outer loop — current row number (1 to rows). |
j | int | Space loop — prints leading spaces while j > i. |
k | int | Number loop — prints digits 1..i. |
for i from 1 to rows:
for j from rows down to i+1: print one space
for k from 1 to i: print k in width 2
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed rows | 1, 1 2, … | Learning and interviews |
| User-input rows | int.TryParse(...) | Configurable triangle size |
| Left-aligned variant | Remove space loop | Contrast with right alignment |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Space loop | for (j = rows; j > i; j--) Console.Write(" "); |
| Number loop | for (k = 1; k <= i; k++) |
| Print number | Console.Write("{0,2}", k); |
| End the row | Console.WriteLine(); |
| Program 42 contrast | Hollow square grid — not an ascending triangle |
Same ascending triangle — different ways to control rows and alignment.
i = 1..rowsOne row per iteration
j > iLeading-space indent
k = 1..iAscending sequence
skip space loopFlush-left triangle
Reach for this pattern when teaching dual inner loops, ascending sequences, and right-aligned console output.
Natural follow-up — moves from a 2D grid with border conditions to a triangle with leading spaces and ascending digits.
Practice separating space printing from number printing before tackling more complex shapes.
Combine loops with ReadLine and TryParse for flexible row counts.
Compare Program 42 (hollow square) and Program 44 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in dual inner loops, formatted output, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the right-aligned number triangle in the browser.
Three complete C# programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print five rows of the right-aligned number triangle with space and number loops.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
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 = rows; j > i; j--)
Console.Write(" ");
for (k = 1; k <= i; k++)
Console.Write("{0,2}", k);
Console.WriteLine();
}
}
}
} When i = 1, the space loop prints four spaces, then 1. When i = 5, no leading spaces — output 1 2 3 4 5 with fixed-width columns.
Read the row count from the console instead of hard-coding 5.
Read rows from the console with safe parsing.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
Console.Write("Enter number of rows: ");
if (!int.TryParse(Console.ReadLine(), out rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
Console.Write(" ");
for (int k = 1; k <= i; k++)
Console.Write("{0,2}", k);
Console.WriteLine();
}
}
}
} Same space-and-number loop core as Example 1; only rows comes from user input instead of being hard-coded as 5.
Remove the space loop to see how right alignment changes the shape.
Same ascending sequence without leading spaces — numbers start flush left.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
for (int k = 1; k <= i; k++)
Console.Write("{0,2}", k);
Console.WriteLine();
}
}
}
} Only the space loop is removed — the number loop and {0,2} formatting stay the same. Compare this flush-left output with Example 1 to see what the space loop contributes.
using System; brings in Console. Set loop variables i, j, k with rows = 5.
for (i = 1; i <= rows; i++) — ascending outer loop; one row per iteration.
for (j = rows; j > i; j--) — prints a single space for right alignment.
for (k = 1; k <= i; k++) then Console.Write("{0,2}", k) — ascending sequence.
Console.WriteLine() ends the row after both inner loops finish.
Total numbers = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5, row i = 3Trace row 3 — space count, numbers printed, and full row output.
| Step | Detail | Output so far |
|---|---|---|
| Space loop | j = 5, 4 — two spaces | |
k = 1 | {0,2} prints 1 | 1 |
k = 2 | {0,2} prints 2 | 1 2 |
k = 3 | {0,2} prints 3 | 1 2 3 |
| WriteLine | End row 3 | 1 2 3 |
Space count per row = rows - i. Numbers per row = i. Total prints = n(n+1)/2 for n rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: remove the space loop and watch the triangle snap left.
Foundation for right-aligned variants with separate space and number loops.
Example: compare with Program 42 (hollow square) and Program 44 next.
Practice {0,2} formatting and fixed-width columns.
Example: change {0,2} to {0,3} for wider spacing on large row counts.
Add leading spaces once the three-loop structure works.
Example: loop k from i down to 1 for a descending row variant.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for rows = 5 — total is 1+2+3+4+5 = 15.
Pair the pattern with TryParse and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C# courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i, the space loop, and the number loop on paper for rows = 3 before coding — watch how the space count shrinks each row.
Small habits that keep number-pattern code clean.
Space loop (j > i) and number loop (k = 1..i) must run in order before WriteLine().
TryParseAvoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
Write the ascending sequence 1..i on paper before coding the number loop.
Trace i = 1..3 on paper before coding the full rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put WriteLine inside the inner loop.
Mistakes that commonly break right-aligned number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use Write("{0,2}", k); WriteLine only after both inner loops.
Without for (j = rows; j > i; j--), every row starts at the left margin.
→ Run the space loop before the number loop on every row.
{0,2} works for rows up to 9; larger counts need {0,3} or wider.
→ Match the format width to the largest digit you will print.
Plain Write(k) makes multi-digit values crowd earlier columns.
→ Use Console.Write("{0,2}", k) for consistent column width.
Letters or empty input throw FormatException.
→ Prefer int.TryParse and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no leading spaces when rows = 1.
Outer loop never runs when rows < 1 — print nothing or show a message.
rows < 1Treat as invalid; re-prompt instead of silent empty output.
Two rows: 1 and 1 2.
Convert.ToInt32 throws — use TryParse.
Total numbers = rows(rows+1)/2 — grows quadratically with rows.
Try these variations to lock in the pattern.
i prints 1 to irows - iTryParse until rows >= 1i = 1..rows. Space loop j = rows..i+1 prints one space. Number loop k = 1..i prints {0,2}.Console.Write stays on the line; WriteLine advances — mix them carefully.rows >= 1 for interactive programs; rows = 1 prints a single 1.rows - i — compare with Example 3 where removing the space loop gives a left-aligned triangle.Quick Takeaway: outer i = 1..rows, space loop j = rows..i+1, numbers k = 1..i with {0,2}, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The right-aligned number triangle is a compact lesson in dual inner loops and formatted output: print spaces while j > i, print 1..i with {0,2}, and end each row with WriteLine(). Master the fixed-rows version, then try user input and the left-aligned contrast.
Practice the three examples above, then continue to Program 44 for the next pattern in the series.
Run the space loop before the number loop — validate rows when reading from the console.
for (i = 1; i <= rows; i++) in the outer loopfor (j = rows; j > i; j--) Console.Write(" ");Console.Write("{0,2}", k) for k = 1..iint.TryParse over bare Convert.ToInt32WriteLine inside an inner loop{0,1} when rows may exceed 9rows = 1 edge casePrint the pattern the beginner-friendly way.
Ascending seq
Definitionj > i spaces
CodeFixed width
CodeWriteLine after j
ShapeO(n²) time
AnalysisEach row prints numbers 1 to i. A space loop runs while j > i before the number loop; {0,2} keeps columns aligned in the console.
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful