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.
Approach
How to Solve It
Pick the row character from parity, then repeat it while the inner bound shrinks.
Method
Idea
Best for
Nested loops + %
Outer picks 1/0; inner repeats it
Learning, interviews, exams
Ternary + spaced
Same shape; ch then a space
Readable 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
Goal
Pattern
Walk each row
for (i = 1; i <= rows; i++)
Pick the character
ch = (i % 2 == 0) ? '0' : '1';
Shrink the row
for (j = i; j <= rows; j++) cout << ch;
End the row
cout << "\n";
Space between chars
cout << ch << " ";
Start with zeros
Swap the ternary arms (odd → '0')
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << ch
Stays on the same line
Each 1 or 0
cout << "\n"
Ends the current line
After 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.
Try it
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 result5 rows · 15 chars
11111
0000
111
00
1
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i, the parity choice, and how many times the character repeats.
i
i % 2
Char
Inner j
Printed row
1
1 (odd)
1
1..4
1111
2
0 (even)
0
2..4
000
3
1 (odd)
1
3..4
11
4
0 (even)
0
4..4
0
Total prints: 4 + 3 + 2 + 1 = 10 = 4×5/2. The character is chosen once per row — the inner loop only repeats it.
Code
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;
}
Output
11111
0000
111
00
1
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;
}
Output (when user enters 4)
Enter the number of rows: 4
1111
000
11
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;
}
Output
1 1 1 1 1
0 0 0 0
1 1 1
0 0
1
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.