Creating an alphabet triangle involves displaying letters in a triangular format where each row contains an increasing number of characters. For instance, the first row has 'A', the second row has 'A B', and so on. This kind of pattern can be easily generated using nested loops in various programming languages. Below are examples of how to achieve this in C, C++, Python, and Java.
#include <stdio.h>
int main() {
int rows, i, j;
char ch;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
ch = 'A';
for (j = 0; j <= i; j++) {
printf("%c ", ch);
ch++;
}
printf("
");
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
for (int i = 0; i < rows; i++) {
char ch = 'A';
for (int j = 0; j <= i; j++) {
cout << ch << " ";
ch++;
}
cout << endl;
}
return 0;
}
rows = int(input("Enter the number of rows: "))
for i in range(rows):
ch = 'A'
for j in range(i + 1):
print(ch, end=" ")
ch = chr(ord(ch) + 1)
print()
import java.util.Scanner;
public class AlphabetTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 0; i < rows; i++) {
char ch = 'A';
for (int j = 0; j <= i; j++) {
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
sc.close();
}
}