Prompt the user for a character and the height of a right triangle. Then, print a triangle of that height using the character entered. Validate that the height entered is > 0. If not, ask the user repeatedly until they comply. Hint: Use nested loops Example Input Corresponding output Enter a character: # #Enter a height: 4 ## ###

Respuesta :

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

%

%%

%%%

%%%%

%%%%%

%%%%%%

%%%%%%%

%%%%%%%%

%%%%%%%%%

Ver imagen nafeesahmed
ACCESS MORE