Answer:
Complete code with comments for explanation and output results are given below
Explanation:
#include <iostream>
using namespace std;
int main()
{
int h;
char ch;
// prompt the user for a character
cout << "Enter the character"<<endl;
cin >> ch;
// prompt the user for the height of the triangle
cout << "Enter the height of the triangle"<<endl;
cin >> h;
// validate the input by checking if it is less or equal to 0
if (h <= 0)
{
cout<<"wrong selection! Enter height again"<<endl;
cin>>h;
}
// run two nested loops one for rows and one for columns to print the triangle
for(int i = 1; i <= h; i++)
{
for(int j = 1; j <= i; j++)
{
cout <<ch;
}
cout << "\n";
}
return 0;
}
Output:
Enter the character
%
Enter the height of the triangle
0
wrong selection! Enter height again
9
%
%%
%%%
%%%%
%%%%%
%%%%%%
%%%%%%%
%%%%%%%%
%%%%%%%%%