c-plus-plus
  1. c-plus-plus-alphabet-triangle

C++ Program to Print Alphabet Triangle

This program will print an alphabet triangle pattern using ASCII values of the alphabets.

Syntax

#include <iostream>
using namespace std;

int main() {
    int rows;
    char alphabet = 'A';

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

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            cout << alphabet << " ";
            alphabet++;
        }
        cout << endl;
    }

    return 0;
}

Example

Suppose we input 4 as the number of rows, the output will be:

A 
B C 
D E F 
G H I J 

Explanation

In the program, we first declare and initialize the variables rows and alphabet. Then we use a nested for loop to print the alphabet triangle pattern.

The outer loop runs rows number of times and the inner loop runs i number of times. Inside the inner loop, we first print the current value of the alphabet variable and then increment it by one.

Use

This program can be used to print alphabet triangles or patterns in various formats as required for specific applications.

Important Points

  • The ASCII value of 'A' is 65.
  • The ASCII value of alphabets follow in sequence from 65.
  • We can use ASCII values to print alphabets in C++.

Summary

In this program, we have demonstrated how to print an alphabet triangle pattern using ASCII values of alphabets in C++. This can be useful in various applications where such patterns are required.

Published on: