Shape Rule
1 or 0 per row
Row 1 prints 11111, row 2 prints 0000, row 3 prints 111 — odd rows use 1, even rows use 0.

The alternating 1 and 0 pattern prints 11111, 0000, 111, 00, 1 — a natural step after Program 39’s rotating number pattern. This tutorial covers nested loops with i % 2 parity, shrinking row lengths, a live preview, algorithm steps, worked C# examples, edge cases, and complexity.
1 or 0 per row
Row 1 prints 11111, row 2 prints 0000, row 3 prints 111 — odd rows use 1, even rows use 0.
i = 1..rows
for (i = 1; i <= rows; i++) walks each row and drives the parity check.
i..rows
for (j = i; j <= rows; j++) repeats the row character rows - i + 1 times.
i % 2
i % 2 == 0 prints 0; otherwise print 1 for the whole row.
3–9 rows
Pick a row count and draw the alternating binary triangle in the browser.
Complexity
Total character prints = n(n+1)/2; extra memory stays O(1).
A alternating 1 and 0 pattern prints only 1 or 0 on each row, alternating by row parity while the row length shrinks. With rows = 5, the output is 11111, 0000, 111, 00, 1.
In C# the outer loop runs i = 1..rows, the inner loop repeats a character rows - i + 1 times, and i % 2 picks 1 or 0 for the whole row.
It combines nested loops with a condition — a key step after Program 39’s rotating rows.
Inner loop runs i..rows.
Odd i → 1; even i → 0.
Program 39 rotates digits; Program 40 alternates binary symbols.
Follow Program 39; continue to Program 41 next.
In short: outer i = 1..rows, inner j = i..rows, print 1 or 0 via i % 2, then WriteLine().
Given a positive integer rows (e.g. 5), print an alternating binary triangle: odd rows are all 1s, even rows are all 0s, with row length decreasing from rows to 1.
// rows = 5
//11111
//0000
//111
//00
//1 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines; also controls the longest row width. |
i | int | Outer loop — current row index; drives parity via i % 2. |
j | int | Inner loop — repeats the row character from i to rows. |
for i from 1 to rows:
ch = "0" if i is even else "1"
for j from i to rows:
print ch
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops + modulo | 11111, 0000, … | Learning and interviews |
| User-input rows | int.TryParse(...) | Flexible console programs |
| Spaced output | Console.Write(ch + " ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= rows; i++) |
| Repeat row char | for (j = i; j <= rows; j++) Console.Write(ch); |
| Pick 1 or 0 | i % 2 == 0 ? "0" : "1" |
| End the row | Console.WriteLine(); |
| Spaced chars | Console.Write(ch + " "); |
| User input | int.TryParse(Console.ReadLine(), out rows) |
| Program 39 contrast | Rotating digits i..rows then i-1..1 — no modulo |
Same alternating 1/0 triangle — different ways to control rows and formatting.
i = 1..rowsWalks each row
j = i..rowsRepeats row character
i % 2Odd → 1, even → 0
j++Inner loop counts up
Reach for this pattern when teaching conditions inside nested loops and shrinking row lengths.
Natural follow-up after Program 39 — same shrinking rows but each line is all 1s or all 0s.
Outer/inner bound practice with an immediate visual check.
Combine loops with ReadLine for a flexible row count.
Compare Program 39 (rotating digits) and Program 41 (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 nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the alternating 1/0 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 alternating 1/0 triangle with nested loops and modulo.
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;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
{
if (i % 2 == 0)
Console.Write("0");
else
Console.Write("1");
}
Console.WriteLine();
}
}
}
} When i = 1 (odd), the inner loop prints 1 five times — output 11111. When i = 2 (even), it prints 0 four times — output 0000. The outer loop increases i each row, shortening the inner loop.
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)
{
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows < 1) return;
for (int i = 1; i <= rows; i++)
{
for (int j = i; j <= rows; j++)
Console.Write(i % 2 == 0 ? "0" : "1");
Console.WriteLine();
}
}
}
} Same inner-loop core as Example 1; only the source of rows changes from a literal to user input.
Add a space between characters for easier reading on each row.
Keep rows = 5 but print each character followed by a space.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
string ch = (i % 2 == 0) ? "0" : "1";
for (int j = i; j <= rows; j++)
{
Console.Write(ch + " ");
}
Console.WriteLine();
}
}
}
} Only the print statement changes — Console.Write(ch + " ") instead of Console.Write(ch). Loop bounds and parity check stay the same as Example 1.
using System; brings in Console. Set loop variables i, j with rows = 5.
for (i = 1; i <= rows; i++) — ascending outer loop walks each row.
for (j = i; j <= rows; j++) — repeats the row character rows - i + 1 times.
i % 2 == 0 prints 0; otherwise print 1 for the whole row.
Console.WriteLine() ends the row after the inner loop finishes.
Rows shrink from rows characters to one — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value of i, the inner-loop range, character count, parity, and full row output.
i | Inner loop (j) | Char | Prints | Row output |
|---|---|---|---|---|
1 | 1, 2, 3, 4, 5 | 1 | 5 | 11111 |
2 | 2, 3, 4, 5 | 0 | 4 | 0000 |
3 | 3, 4, 5 | 1 | 3 | 111 |
4 | 4, 5 | 0 | 2 | 00 |
5 | 5 | 1 | 1 | 1 |
Prints per row = rows - i + 1 — 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: swap the if/else to start rows with 0 instead of 1.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 41 for the next pattern in the series.
Practice Write vs WriteLine without complex math.
Example: put WriteLine inside the inner loop by mistake.
Add spaces between characters once the two-loop structure works.
Example: use Console.Write(ch + " ") between characters on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed characters for rows = 5 — total is 15 (5+4+3+2+1).
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 and j on paper for rows = 3 before coding — watch how each row shortens by one character.
Small habits that keep number-pattern code clean.
Outer loop counts up (i++); inner loop counts from i to rows.
TryParseAvoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
i % 2 == 0 picks 0 for even rows; odd rows print 1.
Trace i = 1, 2, 3 on paper before coding the full rows = 5 demo.
Pro Tip: if the output is a vertical list of single characters per line, you almost certainly put WriteLine inside the inner loop.
Mistakes that commonly break alternating 1/0 number triangles.
Each character lands on its own line — you get a column, not a triangle.
→ Use Write(ch) for characters; WriteLine only after the inner loop.
Checking j % 2 alternates characters within a row — you get 10101, not a uniform row.
→ Check i % 2 once per row, outside or before the inner loop.
for (j = 1; j <= i; j++) grows rows instead of shrinking them.
→ Use for (j = i; j <= rows; j++) so row i prints rows - i + 1 characters.
Swapping odd/even by mistake starts with 0 on row 1 instead of 1.
→ Odd i prints 1: use i % 2 != 0 or else branch for 1.
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.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 11 and 0.
Convert.ToInt32 throws — use TryParse.
Each row prints rows - i + 1 characters — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
0 on row 1 instead of 1Console.Write(ch + " ") between charactersi = 1..rows. Inner loop: j = i..rows with j++. Parity: i % 2.Console.Write stays on the line; WriteLine advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.i prints exactly rows - i + 1 characters — odd rows are 1, even rows are 0.Quick Takeaway: outer loop i = 1..rows, inner loop j = i..rows, print 1 or 0 via i % 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 alternating 1 and 0 pattern is a compact nested-loop lesson: an ascending outer loop shortens each row while i % 2 picks the row character. Master the fixed-rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 41 for the next pattern in the series.
Row i prints rows - i + 1 copies of 1 or 0 — keep WriteLine outside the inner loop and validate row counts when reading input.
for (i = 1; i <= rows; i++) in the outer loopfor (j = i; j <= rows; j++) repeats the row characteri % 2 once per row to pick 1 or 0rows ≥ 1 for interactive programsint.TryParse over bare Convert.ToInt32WriteLine inside the inner character loopj % 2 when you meant row parity on ij = 1..i when rows should shrinkrows = 1 edge casePrint the pattern the beginner-friendly way.
Odd row = 1, even = 0
DefinitionCounts up rows
Codej = i up to rows
Codei % 2 picks char
ModuloO(n²) time
AnalysisOdd rows print 1, even rows print 0 — chosen with i % 2. Row i prints rows - i + 1 characters; total prints = n(n+1)/2.
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful