Program to print Alphabet Triangle in C
×


Program to print Alphabet Triangle in C

174

Program to print alphabet triangle in C using a single loop:

// program to print alphabet triangle in c 
#include<stdio.h> 

int main() {
    int rows, L, M;
    char Word = 'A';

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

    for (L = 1; L <= rows; L++) {
        // Print spaces
        for (M = 1; M <= rows - L; M++) {
            printf(" ");
        }

        // Print alphabets
        for (M = 1; M <= L; M++) {
            printf("%c ",alphabets);           
            alphabets++
            }

        printf("\n");
    }

    return 0;
}

Output:

Enter the number of rows: 4
   A 
  B C 
 D E F 
G H I J 

Description of the program:

Include the standard input-output header file stdio.h.

Declare variables rows, L, M, and Word.

Ask the user to enter the number of rows for the triangle.

The outer loop (for L) runs from 0 to rows-1, representing each row of the triangle.

The first inner loop (for M) prints the spaces before the alphabet.

The number of spaces decreases with each row to form a triangle.

The second inner loop (for M) prints the alphabet.

With each iteration, the alphabet is incremented.

After printing each row, we move to the next line using printf("\n");.

 In this approach, the number of spaces printed before the alphabet in each row decreases with each iteration of the outer loop, while the alphabet is incremented to print the required alphabet in each row.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments