Login Register Appointment

Left Align (1-0) Triangle Pattern

Lesson 8/21 | Study Time: 15 Min
Course: Pattern Program

Pattern:


1

10

101

1010


Solution

Note: Don't jump directly to the solution, try it out yourself first.


Pattern Breakdown

  1. Row 1 → 1
  2. Row 2 → 10
  3. Row 3 → 101
  4. Row 4 → 1010

Observations:

The number of elements in each row equals the row number + 1.

The first column (col = 0) should always be 1, and then it alternates.

If col is even, print 1; if odd, print 0.


Video Solution


Java

public class Main {

    public static void main(String[] args) {

        int total_rows = 4;  // Number of rows


        // Outer loop for rows (starting from 0)

        for (int row = 0; row < total_rows; row++) {

            // Inner loop for columns (starting from 0)

            for (int col = 0; col <= row; col++) {

                // Print '1' for even index, '0' for odd index

                System.out.print(col % 2 == 0 ? "1" : "0");

            }

            System.out.println();  // Move to the next line

        }

    }

}




C++

#include <iostream>

using namespace std;


int main() {

    int total_rows = 4;  // Number of rows


    // Outer loop for rows (starting from 0)

    for (int row = 0; row < total_rows; row++) {

        // Inner loop for columns (starting from 0)

        for (int col = 0; col <= row; col++) {

            // Print '1' for even index, '0' for odd index

            cout << (col % 2 == 0 ? "1" : "0");

        }

        cout << endl;  // Move to the next line

    }


    return 0;

}



Python

total_rows = 4  # Number of rows


# Outer loop for rows (starting from 0)

for row in range(total_rows):

    # Inner loop for columns (starting from 0)

    for col in range(row + 1):

        print("1" if col % 2 == 0 else "0", end="")  # Print '1' for even, '0' for odd

    print()  # Move to the next line