This program will output a right triangle based on user-specified height triangleHeight and symbol triangleChar.
(1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character.
(2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or *. Each subsequent line will have one additional user-specified character until the number in the triangle's base reaches triangleHeight. Output a space after each user-specified character, including a line's last user-specified character.
Example output for triangleChar = % and triangleHeight = 5:
Enter a character: %
Enter triangle height: 5

%
% %
% % %
% % % %
% % % % %

#include
usingnamespacestd;
intmain()
{
char triangleChar='-';
inttriangleHeight=0;
cout<<"Enter a character: "< cin>>triangleChar;

cout<<"Enter triangle height: "<>triangleHeight;
cout<<"@"<<" "< cout<<"@"<<" "<<"@"<<" "< cout<<"@"<<" "<<"@"<<" "<<"@"<<" "<
return0;
}

Respuesta :

Answer:

The above program is not correct, the correct program in c++ langauge is as follows:

#include <iostream>//header file.

using namespace std; //package name.

int main() //main function.

{

  char char_input;//variable to take charater.

  int size,i,j;//variable to take size.

  cout<<"Enter the size and charter to print the traingle: ";//user message.

  cin>>size>>char_input; //take input from the user.

  for(i=0;i<size;i++)//first for loop.

  {

     for(j=0;j<=i;j++)//second for loop to print the series.

       cout<<char_input<<" ";//print the charater.

     cout<<"\n";//change the line.

  }

  return 0; //returned statement.

}

Output:

  • If the user inputs 5 for the size and '%' for the charater, then it will prints the above series example.

Explanation:

  • The above program is written in C++ language, in which there are two for loop which prints the series.
  • The first for loop runs n time, where n is the size given by the user.
  • The second loop is run for every iteration value of the first for loop.
  • For example, if the first for loop runs for the 2 times, then the second for loop runs 1 time for the first iteration of the first loop and 2 times for the second iteration of the first loop.