Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the 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
10
101
1010
10101
Trace
Worked Walkthrough — rows = 5
Trace how an ascending j plus j % 2 builds each binary row that starts with 1.
i
j values
j % 2
Prints
1
1
1
1
2
1, 2
1, 0
10
3
1, 2, 3
1, 0, 1
101
4
1..4
1, 0, 1, 0
1010
5
1..5
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 up 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 = 1; j <= i; j++)
cout << j % 2;
cout << "\n";
}
return 0;
}
Output
1
10
101
1010
10101
How It Works
1. Outer grows the row.i is both the row index and the upper bound of j.
2. Inner maps to binary.j starts at 1, so every row begins with 1 % 2 = 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 = 1; j <= i; j++)
cout << j % 2;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1
10
101
1010
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Outer grow / ascending 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 ascending j % 2.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
cout << j % 2;
cout << "\n";
}
return 0;
}
Output
1
10
101
How It Works
1. Three rows.i = 1 → 1; i = 2 → 10; i = 3 → 101.
2. Trace on paper. If the inner loop counted down instead, row 2 would be 01 (Program 15) — not 10.
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 123 instead of binary
If you print j instead of j % 2, you get ascending integers. Always apply modulo 2.
j = i..1
Got Program 15 instead
A descending inner loop yields 1, 01, 101… Keep j = 1..i so every row starts with 1.
\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 up + modulo: inner j = 1..i with j % 2 builds 1, 10, 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 1 to i, then newline.
Frequently Asked Questions
An alternating binary triangle that starts every row with 1: for rows=5 you get 1, 10, 101, 1010, 10101.
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.
Program 16 counts the inner loop up (j = 1 to i) producing 1, 10, 101…. Program 15 counts down (j = i to 1) producing 1, 01, 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 up from 1 to i, so every row starts with 1 — still O(n²) total prints.