How to print a BinaryTriangle using C#?

A binary triangle is a pattern formed using alternating 0s and 1s arranged in a triangular shape. Each row contains an increasing number of digits, and the pattern alternates between 0 and 1 for each position across all rows.

How It Works

The binary triangle is created using nested loops where the outer loop controls the number of rows, and the inner loop prints the alternating 0s and 1s for each row. A toggle variable switches between 0 and 1 to create the alternating pattern −

Binary Triangle Pattern 1 01 010 1010 10101 010101 0101010 Row 1: 1 digit Row 2: 2 digits Row 3: 3 digits Row 4: 4 digits ... Pattern continues alternating 0 and 1

Syntax

Following is the basic structure for creating a binary triangle −

for (int i = 1; i <= n; i++) {
   for (int j = 1; j <= i; j++) {
      if (toggle == 1) {
         Console.Write("0");
         toggle = 0;
      } else {
         Console.Write("1");
         toggle = 1;
      }
   }
   Console.WriteLine();
}

Using Toggle Variable Approach

This method uses a toggle variable to alternate between 0 and 1 across the entire pattern −

using System;

public class Demo {
   public static void Main() {
      int n = 7;
      int toggle = 0;
      
      for (int i = 1; i <= n; i++) {
         for (int j = 1; j <= i; j++) {
            if (toggle == 1) {
               Console.Write("0");
               toggle = 0;
            } else {
               Console.Write("1");
               toggle = 1;
            }
         }
         Console.WriteLine();
      }
   }
}

The output of the above code is −

1
01
010
1010
10101
010101
0101010

Using Mathematical Approach

This alternative method calculates the binary value based on the sum of row and column indices −

using System;

public class BinaryTriangle {
   public static void Main() {
      int n = 5;
      
      for (int i = 1; i <= n; i++) {
         for (int j = 1; j <= i; j++) {
            int sum = i + j;
            Console.Write(sum % 2);
         }
         Console.WriteLine();
      }
   }
}

The output of the above code is −

0
01
010
0101
01010

Using Starting Value Approach

This method starts each row with a specific value and alternates within the row −

using System;

public class BinaryPattern {
   public static void Main() {
      int n = 6;
      
      for (int i = 1; i <= n; i++) {
         int start = (i % 2 == 1) ? 1 : 0;
         for (int j = 1; j <= i; j++) {
            Console.Write(start);
            start = 1 - start;
         }
         Console.WriteLine();
      }
   }
}

The output of the above code is −

1
01
101
0101
10101
010101

Comparison of Approaches

Approach Logic Starting Pattern
Toggle Variable Global counter alternates across entire pattern Starts with 1
Mathematical Based on sum of row and column indices Starts with 0
Starting Value Each row starts with calculated value, alternates within row Odd rows start with 1, even with 0

Conclusion

Binary triangles in C# can be created using various approaches including toggle variables, mathematical calculations, or row-based starting values. Each method produces slightly different patterns but follows the same principle of alternating 0s and 1s in a triangular arrangement.

Updated on: 2026-03-17T07:04:35+05:30

633 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements