C Binary Rows Pattern (Alternating 1 and 0)

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

What Is This Pattern?

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

Follows the rotating pattern in Program 39; next is Program 41.

How to Solve It

Start with fixed rows = 5, then generalize with scanf — optionally space the characters for easier reading.

MethodIdeaBest for
Parity + shrinki % 2 picks the char; j = i..rowsLearning, interviews, exams
Spaced charactersSame loops; printf("%c ", ch)Easier reading on long rows

Pseudocode

Pseudocode
for i from 1 to rows:
    ch = (i is even) ? '0' : '1'
    for j from i to rows:
        print ch (no newline)
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Pick the characterch = (i % 2 == 0) ? '0' : '1';
Shrink the rowfor (j = i; j <= rows; j++) printf("%c", ch);
End the rowprintf("\n");
Spaced variantprintf("%c ", ch);

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%c", ch) / printf("1")Stays on the same lineEach character in the row
printf("\n")Ends the current lineAfter the inner loop

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

Live Preview

Change the row count and the alternating 1/0 triangle updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 chars
11111
0000
111
00
1

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, the parity choice, and how many times the character repeats.

ii % 2CharCountPrinted row
11 (odd)141111
20 (even)03000
31 (odd)1211
40 (even)010

Total characters for n rows = n(n + 1)/2 (here 10). That triangular sum is why time is O(n²).

C Programs

Three complete programs: fixed rows, scanf input, and a spaced-character variant. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded row count — odd rows print 1, even rows print 0.

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

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

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

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.

Time and Space Complexity

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

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.

Next: Square Numbers Pyramid

Continue with centered rows of square numbers using a running counter.

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