This post is about C++ Program to print alphabets triangle from A to E. We will code for upper case alphabets and lower case alphabets.
A
A B
A B C
A B C D
A B C D E
C++ Program to print alphabets triangle of alphabets A to E
To draw a triangle in c++ we need ASCII code of characters A to E.In this example, we are drawing a triangle of the Upper case letter.
Character | ASCII Value |
A | 65 |
B | 66 |
C | 67 |
D | 68 |
E | 69 |
How does it work
The ASCII values for A to E are given in the above Table.We have used the outer loop for loop for(i=65;i<=69;i++) start from ASCII value of A to E mean(65 to 69).
And the inner loop will also start from 65 and its value remains less than and equal to the value of the variable I
By using the (char) we are converting ASCII value to the character.
We are printing the char to draw a Triangle. The newline character (\n) to add a new line.
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=65;i<=69;i++)
{
for(j=65;j<=i;j++)
{
cout<<(char)j<<"\t";
}
cout<<"\n";
}
return 0;
}
Output
A
A B
A B C
A B C D
A B C D E
C++ Program to draw triangle of lower case letter a to e
In this example, we will do code for how to draw a triangle of lower case letters from a to E.
Character | ASCII Value |
a | 97 |
b | 98 |
c | 99 |
d | 100 |
e | 101 |
How does it work
The ASCII values for A to E are given in the above Table.We have used the outer loop for(i=97;i<=101;i++) start from ASCII value of A to E mean(65 to 69).
And the inner loop will also start from 97 and its value remains less than and equal to the value of the variable I
By using the (char) we are converting ASCII value to the character.
We are printing the char to draw a Triangle. The newline character (\n) to add a new line.
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=97;i<=101;i++)
{
for(j=97;j<=i;j++)
{
cout<<(char)j<<"\t";
}
cout<<"\n";
}
return 0;
}
Output
a
a b
a b c
a b c d
a b c d e