C Program To Display Right Angled Triangle With Alphabets, using For Loop


Lets write a C program to display/print/output a right angled triangle pattern formed with Capital Letter English Alphabets, using nested for loop.

Related Read:
Nested For Loop In C Programming Language
C Program To Display Right Angled Triangle With Alphabets, using While Loop

Expected Output for the Input

If user enters number of rows as 5. Then our C program prints a Right angled Triangle with 5 rows of Capital Letter English Alphabets in it.

User Input:
num = 5;

Output:

A
B C
D E F
G H I J
K L M N O

Video Tutorial: C Program To Display Right Angled Triangle With Alphabets, using For Loop


[youtube https://www.youtube.com/watch?v=WYDm6zrPhkg]

YouTube Link: https://www.youtube.com/watch?v=WYDm6zrPhkg [Watch the Video In Full Screen.]

Logic To Display Right Angled Triangle With Alphabets, using For Loop

If we take a closer look at the right angled triangle, we can see that the number of rows and the number of elements in that row are same. Example, row number 5 has 5 elements. Row number 6 has 6 elements etc. So we store this number in a variable called row. Since row starts from 1, we initialize the variable row to 1.

Outer For Loop

We ask the user to enter the maximum number of rows for the right angled triangle, and store it in address of variable num. Outer for loop selects the row. Outer for loop executes until row value is less than or equal to the user entered number.

Inner For Loop

Inner For loop prints the Alphabets. Printing starts from 1st position. So we initialize variable col to 1. And inner for loop iterates until col is less than or equal to the value present in variable row.

Inside inner for loop we check if the value of variable count is 91(ASCII Value of Z is 91). If true, we resent the value of count to 65(ASCII Value of A is 65).

Note: Row number is equal to the number of elements present in that row.

Source Code: C Program To Display Right Angled Triangle With Alphabets, using For Loop

#include<stdio.h>

int main()
{
    int num, row, col, count = 65;

    printf("Enter no of rows\n");
    scanf("%d", &num);

    printf("\n");
    for(row = 1; row <= num; row++)
    {
        for(col = 1; col <= row; col++)
        {
            if(count == 91)
                count = 65;

            printf("%c  ", count++);
        }
        printf("\n");
    }

    return 0;
}

Output 1:
Enter no of rows
5

A
B C
D E F
G H I J
K L M N O

Output 2:
Enter no of rows
10

A
B  C
D  E  F
G  H  I  J
K  L  M  N  O
P  Q  R  S  T  U
V  W  X  Y  Z  A  B
C  D  E  F  G  H  I  J
K  L  M  N  O  P  Q  R  S
T  U  V  W  X  Y  Z  A  B  C

Related Read:
C Program To Print All ASCII Characters and Value using For Loop

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *