C Binary Number Triangle Pattern (Alternating)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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…).

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").

MethodIdeaBest for
j % 2Descending inner loop; remainder is the binary digitLearning, interviews, exams
1 - (j % 2)Same loops; every digit invertedQuick 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

GoalPattern
Grow each rowfor (i = 1; i <= rows; i++)
Print binary digitfor (j = i; j >= 1; j--) printf("%d", j % 2);
End the rowprintf("\n");
Flip every digitprintf("%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

APIEffectUse for
printf("%d", j % 2)Stays on the same lineEach binary digit
printf("\n")Ends the current lineAfter the inner loop

Print all digits without a newline, then end the row once.

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 result 5 rows · 15 digits
1
01
101
0101
10101

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, the descending j sequence, and j % 2.

iInner jj % 2Printed row
1111
22, 10, 101
33, 2, 11, 0, 1101
44, 3, 2, 10, 1, 0, 10101

Total digits are 1+2+3+4 = 10 — the triangular number n(n+1)/2.

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;
}

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;
}

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;
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Column-Wise Alternating Binary

Count the inner loop up to get 1, 10, 101, 1010…

Program 16 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful