Chapter 6:
Looping Structures
Programming Exercise
Problem # 44:
Write a program that inputs the height of a triangle and displays it using loop. For example, if the user enters height as 5, the program should print the following:
&&&&&&&&&
&&&&&&&
&&&&&
&&&
&
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i, j, n, k, m=1, l;
cout<<"Enter the height of triangle:";
cin>>n;
for(j=1;j<=n;j++)
{
m=m+2;
}
for(i=1;i<=n;i++)
{
for(k=i;k>1;k--)
{
cout<<" ";
}
for(l=i;l<(m-i);l++)
{
cout<<"&";
}
k--;
cout<<"\n";
}
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int i, j, n, k, m=1, l;
cout<<"Enter the height of triangle:";
cin>>n;
for(j=1;j<=n;j++)
{
m=m+2;
}
for(i=1;i<=n;i++)
{
for(k=i;k>1;k--)
{
cout<<" ";
}
for(l=i;l<(m-i);l++)
{
cout<<"&";
}
k--;
cout<<"\n";
}
getch();
}
Output:
Let me know in the comment section if you have any question.
Previous Post:
43. Program to print the following output using loop.
Comments
Post a Comment