Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Pattern 1
3.
Pattern 2
4.
Pattern 3
5.
Pattern 4
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

C++ Program to Print Alphabet Triangle

Introduction

The pattern codes serve as crucial interview questions in many companies to date. Alphabet triangle patterns are one of those important questions. In this tutorial, we will discuss a few concepts of C++ programming language to print triangle alphabet patterns using looping statements.

Here, we displayed various alphabet triangle patterns using nested for loop of C++ language. We receive the user input and print the pattern accordingly. 

Pattern 1

A
BB
CCC
DDDD
EEEEE

Code

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
 int i,j,rows;
 rows=6;
 for(i=1; i<=rows; i++){
    for(j=1; j<=i; j++){
    cout<<((char)(i+64));
 }
 cout<< endl;
 }
getch();
return 0;
}

Output

A
BB
CCC
DDDD
EEEEE
FFFFFF

Pattern 2

AAAAA
BBBB
CCC
DD
E

Code

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
 int i,j,rows;
 rows=4;
 for(i=1; i<=rows; i++){
    for(j=i; j<=rows; j++){
    cout<<((char)(i+64));
 }
 cout<< endl;
 }
getch();
return 0;
}

Output

AAAA
BBB
CC
D

Pattern 3

A
AB
ABC
ABCD
ABCDE

Code

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
 int i,j,rows;
 rows=3;
 for(i=1; i<=rows; i++){
   for(j=1; j<=i; j++){
   cout<<((char)(j+64));
 }
 cout<< endl;
 }
getch();
return 0;
}

Output

A
AB
ABC

 

Try and compile by yourself with the help of online C++ Compiler for better understanding.

Pattern 4

A
BA
CBA
DCBA
EDCBA

Code

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
  int i,j,rows;
  rows=6;
  for(i=1; i<=rows; i++){
  for(j=i; j>=1; j--){
  cout<<((char)(j-1+65));
  }
 cout<<"\n";
}
 getch();
 return 0;
}

Output

A
BA
CBA
DCBA
EDCBA
FEDCBA

 

Also check out this article - Pair in C++

FAQs

  1. How to check an alphabet in C++?
    ASCII values of capital letters range from 65 to 90, and that of small letters range from 97 to 122. So if the integer value of the entered character ranges from 65 to 90 or 97 to 122, then it is an alphabet; otherwise, it is not.
     
  2. What is the ASCII value range of alphabets?
    ASCII values of capital letters range from 65 to 90, and that of small letters range from 97 to 122.

Key Takeaways

In this blog, we learnt about the approaches to different alphabet triangle patterns and wrote the codes for the same using C++ language. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our  Coding Ninjas Blog site and visit our Library. Do upvote our blog to help other ninjas grow.

Recommended Readings:


Happy Learning! 

Live masterclass