An alternating 1 and 0 pattern prints a shrinking triangle where odd rows are all 1s and even rows are all 0s. Parity from i % 2 picks the character for the whole row.
Remember
Rule: for i from 1 to rows
ch = (i even) ? 0 : 1
print ch, (rows - i + 1) times
11111
0000
111
00
1 ← rows = 5
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
{
if (i % 2 == 0)
printf("0");
else
printf("1");
}
printf("\n");
}
return 0;
}
Output
11111
0000
111
00
1
How It Works
1. Outer loop raises i. Each pass shortens the inner loop by one and flips parity.
2. Parity picks the digit. Even i prints 0; odd i prints 1 for every column.
3. End the line.printf("\n") runs only after the inner loop finishes.
When i = 1 you get 11111; when i = 2 you get 0000.
Example 2 — User Input Version
Read rows with scanf and reject invalid input. Same parity-and-shrink core.
C
#include <stdio.h>
int main(void)
{
int rows;
int i, j;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows < 1)
return 0;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
printf(i % 2 == 0 ? "0" : "1");
printf("\n");
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1111
000
11
0
How It Works
1. Prompt and validate. Exit early if scanf fails or rows < 1.
2. Same core. A ternary replaces the if/else — only the hard-coded 5 becomes the user value.
3. Safer input tip. Prefer an explicit message instead of a silent exit:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Enter a positive whole number of rows.\n");
return 1;
}
Example 3 — Spaced characters
Keep rows = 5; pick ch once per row and print it with a trailing space.
C
#include <stdio.h>
int main(void)
{
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++)
printf("%c ", ch);
printf("\n");
}
return 0;
}
Output
1 1 1 1 1
0 0 0 0
1 1 1
0 0
1
How It Works
1. Pick once per row. Set ch before the inner loop so parity is not recomputed every column.
2. Add trailing spaces. Only the print statement changes to printf("%c ", ch).
3. Learn the compact form first. Use Examples 1–2 for the classic no-space triangle; treat spacing as a polish option.
Edge Cases & Pitfalls
Check these before calling the solution done.
j % 2
Alternating inside the row
Checking j % 2 prints 10101 within a row. Use i % 2 once so the whole row shares one character.
swapped
Opposite pattern
If odd rows print 0, you get 00000 first. For this lesson keep odd → 1, even → 0.
\n inside
Broken rows
If printf("\n") sits inside the inner loop, each character lands on its own line. Call the newline only after the row finishes.
j = 1..i
Growing instead of shrinking
An ascending inner bound prints a widening triangle. Keep j = i; j <= rows; j++.
rows = 1
Smallest pattern
Output is a single 1. A good sanity check for parity and the newline.
scanf
Check the return value
If scanf fails, rows may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(rows²)
O(1)
Spaced characters (Example 3)
O(rows²)
O(1)
Total characters = n(n + 1)/2 (triangular). For rows = 5 that is 15 printed characters.
Remember
Key Takeaways
Rule: odd i → 1, even i → 0; print it rows - i + 1 times.
Parity on i: check the row index, not the column index, so the whole row stays uniform.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time from the triangular sum; O(1) extra space.
One line: for each i, print 1 or 0 via i % 2 across j = i..rows, then printf("\n").
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 printf("%c ", ch) instead of printf("%c", ch) — see Example 3.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Check scanf's return value and reject non-positive row counts so the outer loop has a valid range.
🤔
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.