Column-Wise Alternating Binary Triangle in C++

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

What You’ll Learn

How to print a binary number triangle where digits alternate by position (column):

1, 10, 101, 1010, 10101

The core idea is printing j % 2 while j counts up from 1.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
10
101
1010
10101
1

Complete C++ Program

The outer loop controls the number of rows. For each row i, the inner loop runs from 1 to i and prints j % 2 to form 1010....

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

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

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

    return 0;
}

🧠 How It Works

1

Set the row count

int rows = 5; sets how many lines the triangle prints.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) increases row length from 1 to rows.

Row control
3

Inner loop (print 1010...)

for (j = 1; j <= i; j++) prints j % 2, giving 1 for odd positions and 0 for even positions.

Digit printing
4

New line

cout << "\\n"; moves to the next row.

Line break
=

Column-wise alternating triangle

Total printed digits are 1+2+…+n = n(n+1)/2, so the time complexity is O(n²).

2

Variation — User Input Version

Let the user choose the number of rows at runtime. If the input is invalid or non-positive, we stop early.

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

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

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

    if (!cin || rows <= 0) return 0;

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Invert the digits by printing 1 - (j % 2)
  • Add spaces: cout << (j % 2) << ' ';
  • Right-align the triangle by printing leading spaces before each row
  • Increase rows for a bigger pattern
  • Replace digits with characters to create alternating alphabet patterns

Avoid

  • Using endl inside loops (unnecessary flushing)
  • Not checking user input before using rows
  • Mixing row/column responsibilities between loops
  • Forgetting the newline after each row

Key Takeaways

1

The outer loop grows the triangle from 1 row item up to rows.

2

j % 2 produces an alternating sequence by position: 1,0,1,0,...

3

Counting j upward makes row 2 print 10 (instead of 01).

4

Time complexity is O(n²) because you print about \(n(n+1)/2\) digits.

❓ Frequently Asked Questions

Because the inner loop prints positions from 1 to i. For row 2: 1 % 2 = 1 and 2 % 2 = 0, producing 10.
Here the inner loop counts up (j = 1..i), so row 2 becomes 10. In Program 15, counting down makes row 2 become 01.
Print 1 - (j % 2) to invert all digits, or start j from 0 and print j % 2.
O(n²) for n rows, because the total digits printed are 1+2+…+n = n(n+1)/2.

Explore More C++ Number Patterns!

Small variations (like loop direction) can create a completely different-looking pattern.

All Number Patterns →
Did you know?

For alternating patterns, you can often replace a full if condition with a single expression like j % 2.

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