C++ Binary Rows Pattern (Alternating 1 and 0)

Beginner
6 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 fill with 1 and even rows fill with 0 — chosen once per row with i % 2.

Remember
Rule: odd i → print '1', even i → print '0';
      row i prints (rows - i + 1) times

11111
0000
111
00
1         ← 5 rows

Unlike Program 39 (rotating digits 1..rows each row), this shape uses only one character per row and shortens like a descending triangle.

How to Solve It

Pick the row character from parity, then repeat it while the inner bound shrinks.

MethodIdeaBest for
Nested loops + %Outer picks 1/0; inner repeats itLearning, interviews, exams
Ternary + spacedSame shape; ch then a spaceReadable demos once the shape clicks

Pseudocode

Pseudocode
for i from 1 to rows:
    ch = '0' if i is even else '1'
    for j from i to rows:
        print ch (no newline)
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Pick the characterch = (i % 2 == 0) ? '0' : '1';
Shrink the rowfor (j = i; j <= rows; j++) cout << ch;
End the rowcout << "\n";
Space between charscout << ch << " ";
Start with zerosSwap the ternary arms (odd → '0')

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << chStays on the same lineEach 1 or 0
cout << "\n"Ends the current lineAfter the inner loop

Print characters without a newline, then end the row once. cout << endl also ends the line and flushes; "\n" is enough for these demos.

Live Preview

Change the row count and the alternating 1/0 triangle updates instantly — including the triangular character total.

Whole numbers from 1 to 15. 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 each outer-loop value of i, the parity choice, and how many times the character repeats.

ii % 2CharInner jPrinted row
11 (odd)11..41111
20 (even)02..4000
31 (odd)13..411
40 (even)04..40

Total prints: 4 + 3 + 2 + 1 = 10 = 4×5/2. The character is chosen once per row — the inner loop only repeats it.

C++ Programs

Three complete programs: fixed rows, cin input, and spaced characters. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded row count — ideal for first demos and screenshots.

C++
#include <iostream>
using namespace std;

int main()
{
    int rows = 5;
    int i, j;

    for (i = 1; i <= rows; i++)
    {
        for (j = i; j <= rows; j++)
        {
            if (i % 2 == 0)
                cout << '0';
            else
                cout << '1';
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop walks rows. i runs from 1 to rows — one shrinking line per iteration.

2. Parity picks the character. Even i prints '0'; odd i prints '1'.

3. Inner loop shrinks. j runs from i to rows, so row i prints rows - i + 1 characters.

When i = 1 you get 11111; when i = 2 you get 0000.

Example 2 — User Input Version

Read the row count at runtime and use a ternary for the character. Prefer validating cin (tip below).

C++
#include <iostream>
using namespace std;

int main()
{
    int rows;
    int i, j;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = 1; i <= rows; i++)
    {
        for (j = i; j <= rows; j++)
            cout << (i % 2 == 0 ? '0' : '1');

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and read. Ask for a row count, then store it in rows.

2. Same parity core. Only the source of rows changes — the print logic matches Example 1.

3. Safer input tip. Prefer:

Safer input
if (!(cin >> rows) || rows < 1)
{
    cout << "Enter a whole number of rows (1 or more).\n";
    return 1;
}

Example 3 — Spaced Characters

Pick ch once per row, then print it with a trailing space for easier reading.

C++
#include <iostream>
using namespace std;

int main()
{
    int rows = 5;
    int i, j;
    char ch;

    for (i = 1; i <= rows; i++)
    {
        ch = (i % 2 == 0) ? '0' : '1';
        for (j = i; j <= rows; j++)
            cout << ch << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

1. Choose once. Set ch before the inner loop so you do not recompute parity on every print.

2. Same shrink bounds. j = i..rows still prints rows - i + 1 characters.

3. Only spacing changes. cout << ch << " " inserts a space after each character; loop logic is unchanged.

Edge Cases & Pitfalls

Check these before calling the solution done.

j % 2

Wrong parity variable

Checking j % 2 flips characters within a row. Use i % 2 so the whole row shares one character.

j = 1..i

Growing width

An ascending inner bound grows rows instead of shrinking them. Keep for (j = i; j <= rows; j++).

\n early

Column of digits

If cout << "\n" is inside the inner loop, each character lands on its own line. End the row only after the inner loop.

0-based i

Swapped start

If the outer loop starts at 0, the first row is even and prints zeros. This tutorial uses 1-based rows so the first line is ones.

rows = 1

Single 1

Output is just 1. A good sanity check.

Bad cin

Validate rows

Check cin >> rows and require rows >= 1 before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(rows²)O(1)

Total characters = n + (n - 1) + … + 1 = n(n + 1)/2 — quadratic in n. Only a few integers of extra memory.

Key Takeaways

  • Rule: odd rows print 1, even rows print 0; row i repeats that character rows - i + 1 times.
  • Parity on i: use i % 2, not j % 2, so the whole row shares one character.
  • Break the row: call cout << "\n" only after the inner loop.
  • Complexity: O(n²) time from the triangular print count; O(1) extra space.

One line: for i = 1..rows, pick 1 or 0 from i % 2, print it rows - i + 1 times, then end the line.

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.
Use cout << ch << " " instead of cout << ch — see Example 3.
Printing a character stays on the same line. Printing a newline ends the current line. Characters use cout without a newline; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
After cin >> rows, check failure and require rows ≥ 1 before the outer loop.

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

Continue with the next pattern in the C++ number-pattern series.

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.

12 people found this page helpful