C# Binary Rows Pattern (Alternating 1 and 0)

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

What Is This Pattern?

An alternating 1 and 0 pattern prints a shrinking triangle where odd rows are all 1s and even rows are all 0s, chosen with i % 2.

Remember
Rule: odd i → 1, even i → 0; length = rows - i + 1

11111
0000
111
00
1   ← 5 rows

In C# the outer loop walks each row; the inner loop repeats one character from j = i to rows so the line shrinks each time.

How to Solve It

One nested-loop idea driven by row parity.

MethodIdeaBest for
If/else + moduloEven i → 0; odd i → 1Learning, interviews, exams
Ternary formSame parity check in one expressionShorter demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        if i is even: print 0 else print 1
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Shrink the rowfor (j = i; j <= rows; j++)
Pick 1 or 0if (i % 2 == 0) Console.Write("0"); else Console.Write("1");
Ternary formConsole.Write(i % 2 == 0 ? "0" : "1");
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach 1 or 0
Console.WriteLineEnds the current lineAfter the inner loop

Live Preview

Change the row count and the alternating binary triangle updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 chars
11111
0000
111
00
1

Worked Walkthrough — rows = 4

Trace parity and length. Odd rows use 1; even rows use 0.

iParity / countPrinted row
1odd → 1 × 41111
2even → 0 × 3000
3odd → 1 × 211
4even → 0 × 10

Row length is rows - i + 1. The character depends only on i, not on j.

C# Programs

Three complete programs: fixed height with if/else, user input with a ternary, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

Shrinking inner loop; even rows print 0, odd rows print 1.

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 <= rows; j++)
                {
                    if (i % 2 == 0)
                        Console.Write("0");
                    else
                        Console.Write("1");
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i grows from 1 to 5 and decides both the parity and the start of the inner loop.

2. Parity. i % 2 == 0 means even → print 0; otherwise print 1.

3. Inner loop. j runs from i to rows, repeating that character rows - i + 1 times.

Example 2 — User Input

Read the row count and use a ternary for compact 1-or-0 logic.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j;

            Console.Write("Enter 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 <= rows; j++)
                    Console.Write(i % 2 == 0 ? "0" : "1");

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

2. Same pattern. Four rows end on a single 0.

Example 3 — Compact rows = 3

A smaller fixed demo — same parity-and-shrink idea, easier to trace by hand.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 3;
            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();
            }
        }
    }
}

How It Works

1. Same rules. Odd rows print 1; even rows print 0; length shrinks each time.

2. Quick check. Three rows end on a single 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

flip parity

Swap even/odd outputs

Printing 0 on odd rows flips the pattern to start with zeros. Keep even → 0, odd → 1.

wrong length

Use j = 1..i instead

That grows rows instead of shrinking them. Keep for (j = i; j <= rows; j++).

check j

Test j % 2 instead of i % 2

That alternates inside the row (10101). This pattern uses one character per entire row.

rows = 1

Single 1

Output is just 1 — one odd row of length 1.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
If/else (Examples 1, 3)O(n²)O(1)
Ternary form (Example 2)O(n²)O(1)

Total printed characters are n + (n-1) + … + 1 = n(n+1)/2, which is quadratic in n.

Key Takeaways

  • Rule: odd i prints 1; even i prints 0.
  • Length: row i has rows - i + 1 characters via j = i..rows.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
  • Next step: Program 41 prints square numbers in a pyramid pattern.

One line: for each row, pick 1 or 0 from i % 2 and repeat it while the row length shrinks.

Frequently Asked Questions

It checks i % 2. When i is even, the row prints 0; when i is odd, the row prints 1.
The inner loop runs from j = i to rows, printing rows - i + 1 characters per row — decreasing from rows down to 1.
Swap the if/else outputs, or invert the condition so odd rows print 0 and even rows print 1.
Program 39 rotates digits 1..rows per row. Program 40 prints only 1 or 0 per row based on parity, with shrinking row length.
Replace 5 with rows in the outer loop bound — see Example 2.
Use Console.Write(ch + " ") instead of Console.Write(ch).
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single 1 on one line.

Did you know?

Odd rows print 1, even rows print 0 — chosen with i % 2. Row i prints rows - i + 1 characters; total prints = n(n+1)/2.

Next: Square Numbers Pyramid

Print perfect squares arranged in a growing pyramid.

Program 41 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.

6 people found this page helpful