Alternating 1 and 0 Descending Pattern in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a descending triangle where each row contains only one digit that alternates by row:

  • Odd rows print 1
  • Even rows print 0

You’ll use nested loops for the decreasing length and i % 2 to choose between 1 and 0.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
11111
0000
111
00
1
1

Complete C++ Program

The inner loop prints rows - i + 1 digits. The digit depends on whether the row number i is odd or even.

C++
#include <iostream>
using namespace std;

int main() {
    int rows = 5;
    int i, j;

    for (i = 1; i <= rows; i++) {
        for (j = i; j <= rows; j++) {
            if (i % 2 == 0)
                cout << "0";
            else
                cout << "1";
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Decide the size

rows = 5 sets the first row length and the total number of rows.

Setup
2

Outer loop chooses the row number

for (i = 1; i <= rows; i++) moves row by row from 1 to rows.

Row control
3

Inner loop creates decreasing length

for (j = i; j <= rows; j++) runs fewer times each row, so the length becomes rows-i+1.

Triangle shape
4

Pick 1 or 0 using parity

If i % 2 == 0 (even row), print 0; otherwise print 1.

Even / odd
=

Alternating rows

Total prints follow a triangular number: n(n+1)/2, so the time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user choose the number of rows using cin:

C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    int i, j;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = 1; i <= rows; i++) {
        for (j = i; j <= rows; j++) {
            cout << ((i % 2 == 0) ? "0" : "1");
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print spaces between digits for readability (e.g., cout << digit << ' ')
  • Swap the alternation (start with 0 on the first row)
  • Make it left-aligned or right-aligned by adding leading spaces
  • Change digits to characters (e.g., X and O) for a different look
  • Validate user input (positive rows) before printing

Avoid

  • Forgetting the newline after each row
  • Hard-coding 5 when you already have a rows variable
  • Using endl in tight loops (it flushes output)
  • Allowing rows to be 0 or negative without checks

Key Takeaways

1

The outer loop chooses the row index (i).

2

The inner loop controls the decreasing length (rows-i+1 prints).

3

i % 2 cleanly alternates between printing 1 and 0.

4

Total prints are n(n+1)/2, so runtime is O(n²).

❓ Frequently Asked Questions

Because we check whether the row number i is even or odd. Even rows print 0, odd rows print 1.
The inner loop runs from j = i to rows, so row 1 prints 5 digits, row 2 prints 4, and so on.
Invert the parity rule: print 0 when i is odd and 1 when i is even.
O(n²) for n rows because the total characters printed are n(n+1)/2.

Explore More C++ Number Patterns!

Once you can control loop bounds and parity, you can build many alternating and zig-zag patterns.

All Number Patterns →
Did you know?

This is the same triangle-length logic as printing 12345, 1234, ... but with the digit fixed per row. Swapping digits (like 2/9) is just changing what you print inside the inner loop.

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