C# Binary Number Triangle Pattern (Starting with 1)
Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An ascending binary number triangle grows each row by one digit while printing 0 and 1 via j % 2 as the inner loop counts up — so every row starts with 1.
Remember
Rule: for i = 1..n, for j = 1..i print (j % 2)
1
10
101
1010
10101 ← 5 rows
In C# you solve it with ascending outer and inner loops that print j % 2 with Console.Write, then WriteLine() ends the row. Program 15 is the twin with a descending inner loop (1, 01, 101…).
Approach
How to Solve It
Master ascending j % 2 first, then optionally flip every digit with 1 - (j % 2).
Method
Idea
Best for
j % 2
Ascending inner loop; remainder is the binary digit
Learning, interviews, exams
1 - (j % 2)
Same loops; every digit inverted
Quick flip variant
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print (j % 2) (no newline)
print newline
Cheat sheet
Goal
Pattern
Grow each row
for (i = 1; i <= rows; i++)
Print binary digit
for (j = 1; j <= i; j++) Console.Write(j % 2);
End the row
Console.WriteLine();
Flip every digit
Console.Write(1 - (j % 2));
Descending twin (Program 15)
for (j = i; j >= 1; j--) Console.Write(j % 2);
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each binary digit j % 2
Console.WriteLine
Ends the current line
After the inner loop
Try it
Live Preview
Change the row count and the binary triangle updates instantly — including the total digit count.
Whole numbers from 1 to 15. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
1
10
101
1010
10101
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i, the ascending j sequence, and j % 2.
i
Inner j
j % 2
Printed row
1
1
1
1
2
1, 2
1, 0
10
3
1, 2, 3
1, 0, 1
101
4
1, 2, 3, 4
1, 0, 1, 0
1010
Total digits are 1+2+3+4 = 10 — the triangular number n(n+1)/2.
Code
C# Programs
Three complete programs: fixed height, flipped digits, and row-count input. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop grows the length; ascending inner loop prints j % 2.
C#
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 = 1; j <= i; j++)
{
Console.Write(j % 2);
}
Console.WriteLine();
}
}
}
}
Output
1
10
101
1010
10101
How It Works
1. Grow.for (i = 1; i <= rows; i++) makes each row one digit longer.
2. Modulo digit. When i = 2, j is 1 then 2 → 1 then 0 → 10.
3. Newline.WriteLine() after the inner loop starts the next longer binary row.
Example 2 — Flip with 1 - (j % 2)
Same loops; invert every digit so the first row starts with 0.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(1 - (j % 2));
}
Console.WriteLine();
}
}
}
}
Output
0
01
010
0101
01010
How It Works
1. Invert. Where j % 2 was 1, you print 0, and vice versa.
2. Same shape. Row lengths still grow 1..5 — only the bit polarity flips.
Example 3 — User Input
Read the row count at runtime. Prefer int.TryParse in real apps so bad input does not throw.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j;
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 = 1; j <= i; j++)
{
Console.Write(j % 2);
}
Console.WriteLine();
}
}
}
}
2. Same nested loops. Four rows start at 1 and end at 1010.
Edge Cases & Pitfalls
Check these before calling the solution done.
forgot %
Write(j) instead of Write(j % 2)
That prints 1, 2, 3… instead of binary digits. Always take modulo 2.
wrong direction
for (j = i; j >= 1; j--)
That is Program 15 (1, 01, 101…). Keep counting up for this lesson’s sample.
WriteLine early
WriteLine(j % 2) inside the inner loop
That puts each digit on its own line. Use Write; call WriteLine() only after the row finishes.
rows = 1
Single digit
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
flip vs base
Mixing Example 1 and 2
1 - (j % 2) is intentional inversion. Do not mix both formulas in the same program unless asked.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
There are n rows printing 1+2+…+n digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for i = 1..n, print j % 2 for j from 1 up to i.
Starts with 1: counting up from j = 1 makes every row begin with an odd remainder.
Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
Next step: Program 17 builds a left-shifted odd number triangle.
One line: for each length from 1 to n, print j % 2 counting up with Write, then WriteLine.
Frequently Asked Questions
Modulo 2 returns the remainder after dividing by 2. Any integer is either even (remainder 0) or odd (remainder 1).
On row 2, the inner loop prints j = 1 then j = 2. That becomes 1 % 2 = 1 then 2 % 2 = 0, so the row is 10.
Console.Write stays on the same line. Console.WriteLine ends the current line. Binary digits use Write(j % 2); the row break uses WriteLine after the inner loop.
Row 3 prints j = 1, 2, 3 and j % 2 becomes 1, 0, 1 — concatenated as 101.
Program 16 counts the inner loop up (j = 1 to i) producing 1, 10, 101, 1010, 10101. Program 15 counts down (j = i to 1) producing 1, 01, 101, 0101, 10101.
Yes. Print 1 - (j % 2) instead of j % 2 to flip every digit — first row becomes 0 instead of 1.
Yes. Toggle a variable between 0 and 1 on each print, but modulo is the simplest approach for this pattern.
O(n²) where n is the number of rows. Total Console.Write calls equal n+(n-1)+…+1 = n(n+1)/2.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Each row prints alternating 0 and 1 using j % 2. The inner loop counts up from 1 to i, so every row starts with 1 — still O(n²) total prints.