Alternating Binary Number Triangle in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Modulo Operator

What You’ll Learn

How to print an alternating binary number triangle in C#. Each row grows by one digit, and the digits alternate using % 2: 1, 01, 101, 0101, 10101.

This is a simple way to practice nested loops and understand how the modulo operator works.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
01
101
0101
10101
1

Complete C# Program

The outer loop controls the row length. The inner loop prints j % 2 while counting down from i to 1.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;
            int i, j;

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j >= 1; j--)
                {
                    Console.Write(j % 2);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Set the number of rows

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

Setup
2

Outer loop (row length)

for (i = 1; i <= rows; i++) makes each next row one digit longer.

Row control
3

Inner loop (alternate 0/1)

j % 2 produces 0 for even j and 1 for odd j, which creates the alternating binary digits.

Modulo
=

Alternating binary triangle

You print 1+2+…+n digits overall, so complexity is O(n²).

2

Variation — Start with 0

If you want the first row to be 0, flip the printed value:

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;

            for (int i = 1; i <= rows; i++)
            {
                for (int j = i; j >= 1; j--)
                {
                    Console.Write(1 - (j % 2));
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Read rows from the console to scale the pattern
  • Add spaces between digits to improve readability
  • Use (i + j) % 2 to alternate by row + column parity

Avoid

  • Forgetting the newline after each row
  • Using string concatenation in tight loops (unnecessary here)

Key Takeaways

1

j % 2 produces alternating 0 and 1.

2

The inner loop direction changes the row’s visual ordering.

3

This is a simple example of using modulo to build patterns.

4

Overall complexity is \(O(n^2)\) for \(n\) rows.

❓ Frequently Asked Questions

Row 3 prints j = 3, 2, 1 and j % 2 becomes 1, 0, 1.
Yes. Keep the inner loop fixed to 5 columns and print (col % 2) or ((col + row) % 2) depending on the style you want.
O(n²) for \(n\) rows since total prints are \(n(n+1)/2\).

Explore More C# Number Patterns!

Modulo-based patterns are a fun way to practice parity, loops, and condition-like behavior.

All Number Patterns →
Did you know?

The modulo operator is used in many places: alternating patterns, checking even/odd numbers, hashing, and circular indexing.

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