An alternating binary number triangle grows each row by one digit while printing 0 and 1 via j % 2 as the inner loop counts down.
Remember
Rule: for i = 1..n, for j = i..1 print (j % 2)
1
01
101
0101
10101 ← 5 rows
In C you solve it with an ascending outer loop and a descending inner loop that prints j % 2 with printf("%d", j % 2), then printf("\n") ends the row. Program 16 flips the inner loop upward (1, 10, 101…).
Approach
How to Solve It
Outer loop grows from 1 to rows. Inner loop counts down and prints j % 2. End each row with printf("\n").
Method
Idea
Best for
j % 2
Descending inner loop; remainder is the binary digit
Learning, interviews, exams
1 - (j % 2)
Same loops; every digit inverted
Quick flip variant
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 1:
print (j % 2) (no newline)
print newline
Cheat sheet
Goal
Pattern
Grow each row
for (i = 1; i <= rows; i++)
Print binary digit
for (j = i; j >= 1; j--) printf("%d", j % 2);
End the row
printf("\n");
Flip every digit
printf("%d", 1 - (j % 2));
Ascending twin (Program 16)
for (j = 1; j <= i; j++) printf("%d", j % 2);
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j % 2)
Stays on the same line
Each binary digit
printf("\n")
Ends the current line
After the inner loop
Print all digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the binary triangle updates instantly — including the total digit count.
Whole numbers from 1 to 15. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
1
01
101
0101
10101
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i, the descending j sequence, and j % 2.
i
Inner j
j % 2
Printed row
1
1
1
1
2
2, 1
0, 1
01
3
3, 2, 1
1, 0, 1
101
4
4, 3, 2, 1
0, 1, 0, 1
0101
Total digits are 1+2+3+4 = 10 — the triangular number n(n+1)/2.
Code
C Programs
Three complete programs: fixed height, flipped digits, and scanf input. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop grows the length; descending inner loop prints j % 2.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j >= 1; j--)
printf("%d", j % 2);
printf("\n");
}
return 0;
}
Output
1
01
101
0101
10101
How It Works
1. Grow.for (i = 1; i <= rows; i++) makes each row one digit longer.
2. Modulo digit. When i = 2, j is 2 then 1 → 0 then 1 → 01.
3. Newline.printf("\n") after the inner loop starts the next longer binary row.
Example 2 — Flip with 1 - (j % 2)
Same loops; invert every digit so the first row starts with 0.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j >= 1; j--)
printf("%d", 1 - (j % 2));
printf("\n");
}
return 0;
}
Output
0
10
010
1010
01010
How It Works
1. Invert. Where j % 2 was 1, you print 0, and vice versa.
2. Same shape. Row lengths still grow 1..5 — only the bit polarity flips.
Example 3 — User Input
Read the row count with scanf and reject non-positive values.
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)
{
printf("Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = i; j >= 1; j--)
printf("%d", j % 2);
printf("\n");
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1
01
101
0101
How It Works
1. Validate rows. Require scanf success and rows >= 1.
2. Same nested loops. Four rows start at 1 and end at 0101.
Edge Cases & Pitfalls
Check these before calling the solution done.
forgot %
printf("%d", j) instead of j % 2
That prints 3, 2, 1… instead of binary digits. Always take modulo 2.
wrong direction
for (j = 1; j <= i; j++)
That is Program 16 (1, 10, 101…). Keep counting down for this lesson’s sample.
\n inside
printf("\n") inside the inner loop
That puts each digit on its own line. Print digits without a newline; call printf("\n") only after the row finishes.
rows = 1
Single digit
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
flip vs base
Mixing Example 1 and 2
1 - (j % 2) is intentional inversion. Do not mix both formulas in the same program unless asked.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
There are n rows printing 1+2+…+n digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for i = 1..n, print j % 2 for j from i down to 1.
Modulo:j % 2 is the binary digit — even → 0, odd → 1.
printf vs newline: digits stay on the line; printf("\n") advances after each row.
Next step: Program 16 counts the inner loop up for a related binary triangle.
One line: for each length from 1 to n, print j % 2 counting down with printf, then printf("\n").
Frequently Asked Questions
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.
printf("%d", j % 2) stays on the same line. printf("\n") ends the current line. Binary digits use printf; the row break uses printf("\n") after the inner loop.
Row 3 prints j = 3, 2, 1 and j % 2 becomes 1, 0, 1 — concatenated as 101.
Program 15 counts the inner loop down (j = i to 1) producing 1, 01, 101, 0101, 10101. Program 16 counts up (j = 1 to i) producing 1, 10, 101, 1010, 10101.
Yes. Print 1 - (j % 2) instead of j % 2 to flip every digit — first row becomes 0 instead of 1.
O(n²) where n is the number of rows. Total digit prints equal n+(n-1)+…+1 = n(n+1)/2.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
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.