Â
In this, we are going to see how to print the above pattern of Triangle of Alphabets, using nested for loops in C++.
The code given below can be used for TURBO C++ Compiler:-
//patern 16
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int number = 'A',i,j,n;
cout<<"Enter number of rows";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
// (char)-this keyword convert the ASCII value to the Alphabets.
cout<<(char)number;
number = (char)number +1;
}
cout<<"\n";
}
getch();
}
The code given below can be used for g++/gcc Compiler:-
// patern 16
#include<iostream>
using namespace std;
int main()
{
int number = 'A',i,j,n;
cout<<"Enter number of rows";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
// (char)-this keyword convert the ASCII value to the Alphabets.
cout<<(char)number;
number = (char)number +1;
}
cout<<"\n";
}
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP