C++ program to print square pattern from ‘1’
In this article, we are going to write the C++ program in which we are going to print the pattern,
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
If you observe the pattern carefully, you will find that the pattern is in square shape and that in each row there are the same number of elements and each column as well. So basically the pattern is very simple and easy you can easily explore this. Before moving toward the program let’s see the input and output of the program.
C++ program to print square pattern from ‘1’
Example:
Input-Please enter any side of square:3
Output-1 1 1
1 1 1
1 1 1
Logic:
Initially, here we have started the loop from zero, row=0, then we will apply the condition that row<side, then we have increment the row, then we have applied the inner loop for a column: initially, we have kept col =0 then we have applied the condition that is col<side then we have increment the column.
Program-
#include<iostream>
using namespace std;
int main()
{
int row,col,side;
cout<<“Please enter any side of square:”;
cin>>side;
for(row=0;row<side;row++)
{
for(col=0;col<side;col++){
cout<<” 1 “;
}
cout<<“n”;
}
return 0;
}
Output-
Please enter any side of square:5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Program Explanation-
- First of all, we declared variables to store the values of side and ‘row’ ‘col’ for the iteration of loops.
- Then we have taken input from the user on the side of the square.
- Then we started the for loop to print the horizontal sides of the square.
- Then we started another for loop inside the upper loop to print the vertical sides of the square.
- Then we printed the ‘1’ pattern of the program.
- In the end, we have applied a new line character ” n”.
Conclusion-
In this article, we have written the C++ program in which we are going to print the pattern. If you study the program carefully and understand its logic you will find it easy once you understand the logic you can easily explore the program.