Google News
logo
C Program to print Alphabet Triangle
There are different triangles that can be printed. Triangles can be generated by alphabets or numbers. In the following example of C program to print an alphabet triangle :
Program :
#include <stdio.h>

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

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%c ", current);
            current++;
        }
        printf("\n");
    }

    return 0;
}
Output :
Enter the number of rows: 6
A 
B C 
D E F 
G H I J 
K L M N O 
P Q R S T U 
Explanation :

* We start by declaring two variables: rows to store the number of rows in the triangle, and current to store the current alphabet character to be printed.

* We ask the user to enter the number of rows using printf and scanf.

* We use two nested for loops to print the triangle. The outer loop controls the number of rows in the triangle, and the inner loop controls the number of characters to be printed in each row.

* Inside the inner loop, we print the current alphabet character using printf and then increment current to print the next character in the next iteration of the loop.

* After the inner loop, we print a newline character using printf to move to the next row of the triangle.

* After the outer loop, we return 0 to indicate successful execution of the program.

Note : This program assumes that the input rows variable is a positive integer. If the input is negative or zero, the program may produce unexpected results.