C# Binary Number Triangle Pattern (Alternating)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An alternating binary number triangle grows each row by one digit while printing 0 and 1 via j % 2 as the inner loop counts down.

Remember
Rule: for i = 1..n, for j = i..1 print (j % 2)

1
01
101
0101
10101   ← 5 rows

In C# you solve it with an ascending outer loop and a descending inner loop that prints j % 2 with Console.Write, then WriteLine() ends the row. Program 16 flips the inner loop upward (1, 10, 101…).

How to Solve It

Master j % 2 first, then optionally flip every digit with 1 - (j % 2).

MethodIdeaBest for
j % 2Descending inner loop; remainder is the binary digitLearning, interviews, exams
1 - (j % 2)Same loops; every digit invertedQuick flip variant

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i down to 1:
        print (j % 2) (no newline)
    print newline

Cheat sheet

GoalPattern
Grow each rowfor (i = 1; i <= rows; i++)
Print binary digitfor (j = i; j >= 1; j--) Console.Write(j % 2);
End the rowConsole.WriteLine();
Flip every digitConsole.Write(1 - (j % 2));
Ascending twin (Program 16)for (j = 1; j <= i; j++) Console.Write(j % 2);

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach binary digit j % 2
Console.WriteLineEnds the current lineAfter the inner loop

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 result 5 rows · 15 digits
1
01
101
0101
10101

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, the descending j sequence, and j % 2.

iInner jj % 2Printed row
1111
22, 10, 101
33, 2, 11, 0, 1101
44, 3, 2, 10, 1, 0, 10101

Total digits are 1+2+3+4 = 10 — the triangular number n(n+1)/2.

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; descending 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 = i; j >= 1; j--)
                {
                    Console.Write(j % 2);
                }
                Console.WriteLine();
            }
        }
    }
}

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 2 then 1 → 0 then 1 → 01.

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 = i; j >= 1; j--)
                {
                    Console.Write(1 - (j % 2));
                }
                Console.WriteLine();
            }
        }
    }
}

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 = i; j >= 1; j--)
                {
                    Console.Write(j % 2);
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 1.

2. Same nested loops. Four rows start at 1 and end at 0101.

Edge Cases & Pitfalls

Check these before calling the solution done.

forgot %

Write(j) instead of Write(j % 2)

That prints 3, 2, 1… instead of binary digits. Always take modulo 2.

wrong direction

for (j = 1; j <= i; j++)

That is Program 16 (1, 10, 101…). Keep counting down 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.

Time and Space Complexity

ProgramTimeExtra 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.

Key Takeaways

  • Rule: for i = 1..n, print j % 2 for j from i down to 1.
  • Modulo: j % 2 is the binary digit — even → 0, odd → 1.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
  • Next step: Program 16 counts the inner loop up for a related binary triangle.

One line: for each length from 1 to n, print j % 2 counting down 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 = 2 then j = 1. That becomes 2 % 2 = 0 then 1 % 2 = 1, so the row is 01.
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 = 3, 2, 1 and j % 2 becomes 1, 0, 1 — concatenated as 101.
Program 15 counts the inner loop down (j = i to 1) producing 1, 01, 101, 0101, 10101. Program 16 counts up (j = 1 to i) producing 1, 10, 101, 1010, 10101.
Yes. Print 1 - (j % 2) instead of j % 2 to flip every digit — first row becomes 0 instead of 1.
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 down from i to 1, so row length grows each line — still O(n²) total prints.

Next: Ascending Inner Binary Triangle

Count the inner loop up to get 1, 10, 101, 1010…

Program 16 tutorial →

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