Chapter 6:
Looping Structures
Programming Exercise
Problem # 47:
Write a program that inputs the height of triangle and displays a triangle of alphabets. For example, if the user enters 5, it displays the following:
A
AB
ABC
ABCD
ABCDE
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
cout<<"Input height of triangle:";
cin>>n;
int i='A';
int a=1;
while(a<=n)
{
for(char k='A';k<=i;k++)
{
cout<<k;
}
cout<<"\n";
a++;
i++;
}
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int n;
cout<<"Input height of triangle:";
cin>>n;
int i='A';
int a=1;
while(a<=n)
{
for(char k='A';k<=i;k++)
{
cout<<k;
}
cout<<"\n";
a++;
i++;
}
getch();
}
Output:
Let me know in the comment section if you have any question.
Previous Post:
46. Program to generate the following pyramid of digits using nested loop.
Comments
Post a Comment