53.2k views
4 votes
Question 4Write a C++ program which display the following shape.
*****

Question 4Write a C++ program which display the following shape. *****-example-1

1 Answer

2 votes

Answer:

The code to this question can be defined as follows:

#include <iostream>//header file

using namespace std;//namespace

int main()//main method

{

int x,y;//defining variable

for(x = 1; x <= 7; x++)//defining outter for loop for print rows

{

for(y = 1; y <= 7; y++)//defining inner for loop for print column

{

if(y <= x)//defining if block to check y less then equal to x

cout << "*";//print asterisk value

else//defining else block

cout << " ";//use for spacing

}

for(y = 7; y >= 1; y--)//defining for loop for print right triangle

{

if(y <= x)//defining if block to check y less then equal to x

cout << "*";//print asterisk value

else//defining else block

cout << " ";//use for spacing

}

cout << "\\";//use for line break

}

return 0;

}

Output:

please find the attachment.

Step-by-step explanation:

In the above program code, two integer variable "x, y" is declared, which is used in the for a loop. In this, two for loop is used to print the left triangle, and the last loop is used to print the right triangle. In both, the loop uses if block to check y is less than equal to x and print the asterisk value with the spacing.

Question 4Write a C++ program which display the following shape. *****-example-1
User Saibal
by
5.5k points