An alternating binary number triangle grows one digit per row, printing j % 2 while j counts down — 1, 01, 101, 0101, 10101.
Remember
Rule: for i from 1 to rows
for j from i down to 1: print j % 2
1
01
101
0101
10101 ← rows = 5
Follows the odd-length descending pyramid in Program 14; next is the ascending-inner binary triangle in Program 16.
Approach
How to Solve It
Outer loop grows row length i. Inner loop counts down from i to 1 and prints j % 2.
Method
Idea
Best for
Descending inner + j % 2
Outer i = 1..rows; inner j = i..1; print j % 2
Learning, interviews, exams
Flip with 1 - (j % 2)
Same loops; invert each digit so row 1 starts with 0
Quick variant once the core is clear
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 1:
print j % 2
print newline
Cheat sheet
Goal
Pattern
Grow each row
for (i = 1; i <= rows; i++)
Print binary digits
for (j = i; j >= 1; j--) cout << j % 2;
End of row
cout << "\n";
Flip all bits
cout << 1 - (j % 2);
vs Program 16
16 uses j = 1..i → starts every row with 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j % 2
Stays on the same line
Each binary digit on the row
cout << "\n"
Ends the current line
After the inner loop finishes
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the alternating binary triangle updates instantly — capped at 9 for readable demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
1
01
101
0101
10101
Trace
Worked Walkthrough — rows = 5
Trace how a descending j plus j % 2 builds each binary row.
i
j values
j % 2
Prints
1
1
1
1
2
2, 1
0, 1
01
3
3, 2, 1
1, 0, 1
101
4
4..1
0, 1, 0, 1
0101
5
5..1
1, 0, 1, 0, 1
10101
Total digits = n(n+1)/2 → O(n²). Always print j % 2, not bare j.
Code
C++ Programs
Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — outer grows; inner counts down and prints j % 2.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j >= 1; j--)
cout << j % 2;
cout << "\n";
}
return 0;
}
Output
1
01
101
0101
10101
How It Works
1. Outer grows the row.i is both the row index and the starting value of j.
2. Inner maps to binary.j % 2 turns each descending j into 0 or 1.
3. Newline. Call cout << "\n" only after the inner loop finishes the row.
Example 2 — User Input Rows
Read rows with cin, validate, then run the same nested loops.
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
cout << "Enter the number of rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = i; j >= 1; j--)
cout << j % 2;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1
01
101
0101
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Outer grow / descending j % 2 matches Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input
if (!(cin >> rows) || rows < 1 || rows > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm descending j % 2.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j >= 1; j--)
cout << j % 2;
cout << "\n";
}
return 0;
}
Output
1
01
101
How It Works
1. Three rows.i = 1 → 1; i = 2 → 01; i = 3 → 101.
2. Trace on paper. If the inner loop counted up instead, row 2 would be 10 (Program 16) — not 01.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
print j
Got 321 instead of binary
If you print j instead of j % 2, you get descending integers. Always apply modulo 2.
j = 1..i
Got Program 16 instead
An ascending inner loop yields 1, 10, 101… Keep j = i..1 for this shape.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.
rows = 1
Single digit
Output is just 1 — a good sanity check for input validation.
flip
Want 0 on the first row?
Use cout << 1 - (j % 2) to invert every bit without changing the loop bounds.
cin
Check the stream
Validate cin >> rows before looping — a failed read leaves rows unset or stale.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Total digits printed = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Grow the row: outer i = 1..rows adds one digit each line.
Count down + modulo: inner j = i..1 with j % 2 builds 1, 01, 101…
Break the row:cout << j % 2 in the inner loop; cout << "\n" after it.
Complexity:O(n²) time from n(n+1)/2 digits; O(1) extra space.
One line: for each i, print j % 2 for j from i down to 1, then newline.
Frequently Asked Questions
An alternating binary triangle: for rows=5 you get 1, 01, 101, 0101, 10101 — each row grows by one digit using j % 2.
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.
Program 15 counts the inner loop down (j = i to 1) producing 1, 01, 101…. Program 16 counts up (j = 1 to i) producing 1, 10, 101….
Yes. Print 1 - (j % 2) instead of j % 2 to flip every digit — first row becomes 0 instead of 1.
cout << (j % 2) stays on the same line. cout << "\n" ends the current line. Binary digits use cout; the row break uses cout << "\n" after the inner loop.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digit prints are 1+2+…+n = n(n+1)/2.
🤔
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.