104k views
5 votes
Write a C++ programthat reads in the side of a square and prints out a pattern on$

symbols representing thatsquare. The program should work for side sizes between

2 and 10. For example, ifa size of 4 is entered, the following output should be

produced:

$$$$

$$$$

$$$$

1 Answer

2 votes

Answer:

#include<iostream>

using namespace std;

//main function

int main(){

//initialize the variables

int side;

//print the message

cout<<"Please enter the side of square: ";

cin>>side; //read the vale and store in the variable.

// for loop

for(int i=1;i<=side;i++){ //lop for rows

for(int j=1;j<=side;j++){ //loop for column

cout<<"$"; //print the '*'

}

cout<<endl;

}

}

Step-by-step explanation:

Create the main function and declare the variable side.

print the message on the screen for the user and then store value enter by the user on the variable side.

take the nested for loop for print the pattern.

nested for loop means, loop inside another loop it is used for print the pattern having rows and columns.

the first loop updates the row number and the second the loop print the character '$' in the column-wise.

in the code,

if i = 1, for loop check the condition 1 <= 5 if the value of side is assume 5.

condition is true and the program moves to the second for loop and starts to print the character '$' five times after that, print the new line.

then, the above process repeat for different rows and finally, we get the pattern in square shape.

User EnKrypt
by
5.2k points