Login Register Appointment

Left Align (A to n Alphabets) Triangle Pattern

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

Pattern:


A

BC

DEF

GHIJ


Solution

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


Approach

1)Require minimum 2 nested loop

2)Outer loop is for row 

3)Inner loop is for column

4)Print in inner loop


Pattern Breakdown

  1. Row 1 → A
  2. Row 2 → BC
  3. Row 3 → DEF
  4. Row 4 → GHIJ

Observations:

  • The letters are printed sequentially from 'A' onward.
  • We need to maintain a counter to track the next character in the sequence.


Video Solution


Java

public class Main {

    public static void main(String[] args) {

        int total_rows = 4;  // Number of rows

        char ch = 'A'; // Starting character


        // Outer loop for rows

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

            // Inner loop for columns (printing letters)

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

                System.out.print(ch);

                ch++;  // Move to the next character

            }

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

        }

    }

}





C++
#include <iostream>

using namespace std;


int main() {

    int total_rows = 4;  // Number of rows

    char ch = 'A'; // Starting character


    // Outer loop for rows

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

        // Inner loop for columns (printing letters)

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

            cout << ch;

            ch++;  // Move to the next character

        }

        cout << endl;  // Move to the next line after each row

    }


    return 0;

}



Python

total_rows = 4  # Number of rows

ch = ord('A')  # Convert 'A' to its ASCII value


# Outer loop for rows

for row in range(0, total_rows ):

    # Inner loop for columns (printing letters)

    for col in range(row):

        print(chr(ch), end="")  # Convert ASCII to character

        ch += 1  # Move to the next character

    print()  # Move to the next line after each row