C Binary Number Triangle Pattern (Starting with 1)

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

What Is This Pattern?

An ascending binary number triangle grows each row by one digit while printing 0 and 1 via j % 2 as the inner loop counts up — so every row starts with 1.

Remember
Rule: for i = 1..n, for j = 1..i print (j % 2)

1
10
101
1010
10101   ← 5 rows

In C you solve it with ascending outer and inner loops that print j % 2 with printf("%d", j % 2), then printf("\n") ends the row. Program 15 is the twin with a descending inner loop (1, 01, 101…).

How to Solve It

Outer loop grows from 1 to rows. Inner loop counts up and prints j % 2. End each row with printf("\n").

MethodIdeaBest for
j % 2Ascending 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 1 to i:
        print (j % 2) (no newline)
    print newline

Cheat sheet

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

Worked Walkthrough — rows = 4

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

iInner jj % 2Printed row
1111
21, 21, 010
31, 2, 31, 0, 1101
41, 2, 3, 41, 0, 1, 01010

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; ascending 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 = 1; j <= i; 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 1 then 2 → 1 then 0 → 10.

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 = 1; j <= i; 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 = 1; j <= i; 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 1010.

Edge Cases & Pitfalls

Check these before calling the solution done.

forgot %

printf("%d", j) instead of j % 2

That prints 1, 2, 3… instead of binary digits. Always take modulo 2.

wrong direction

for (j = i; j >= 1; j--)

That is Program 15 (1, 01, 101…). Keep counting up 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 1 up to i.
  • Starts with 1: counting up from j = 1 makes every row begin with an odd remainder.
  • printf vs newline: digits stay on the line; printf("\n") advances after each row.
  • Next step: Program 17 builds a left-shifted odd number triangle.

One line: for each length from 1 to n, print j % 2 counting up 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 = 1 then j = 2. That becomes 1 % 2 = 1 then 2 % 2 = 0, so the row is 10.
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 = 1, 2, 3 and j % 2 becomes 1, 0, 1 — concatenated as 101.
Program 16 counts the inner loop up (j = 1 to i) producing 1, 10, 101, 1010, 10101. Program 15 counts down (j = i to 1) producing 1, 01, 101, 0101, 10101.
Yes. Print 1 - (j % 2) instead of j % 2 to flip every digit — first row becomes 0 instead of 1.
Yes. Toggle a variable between 0 and 1 on each print, but modulo is the simplest approach for this pattern.
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 up from 1 to i, so every row starts with 1 — still O(n²) total prints.

Next: Left-Shifted Odd Triangle

Print odd numbers that shift left across each row.

Program 17 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