Table of Contents
C++ program to print solid square star pattern
In this article, we will discuss the Program to Print solid square star pattern in C++ programming language
![C Program to print solid square star pattern](https://i0.wp.com/code4javac.com/wp-content/uploads/2019/10/solid-square1.jpg?resize=132%2C147&ssl=1)
In this post, we are going to learn How to write a program to print solid square star pattern in C++ language using for loop, while loop and Do-while loop
Program 1
C++ code to display square pattern Using for loop
This program allows the user to enter the size and then it will display solid square star pattern using for loop in C++ programming language
#include <iostream> #include <conio.h> using namespace std; int main() { int i,j,size; cout << "Please enter the size" << endl; cin>>size;//Takes input from the user for size for(i=1; i<=size; i++){ for(j=1; j<=size; j++){ cout<<"*"; } cout<<"\n"; } getch(); return 0; }
When the above code is executed it produces the following output
![C++ program to print solid square star pattern](https://i0.wp.com/code4javac.com/wp-content/uploads/2019/11/squarepatcpp1.jpg?resize=263%2C248&ssl=1)
Approach
- This program requests the input size
- The input(size) is stored in the variable “size“
- To iterate through the row, run through outer for loop from 1 to given size according to the loop structure for(i=1; i<=size; i++)
- To iterate through the row, run through outer for loop from 1 to given size according to the loop structure for(j=1; j<=size; j++) ;
- inside inner loop print star “*”;
- This activity continues until the condition of outer while loop becomes false.
Program 2
C++ code to display square pattern Using while loop
This program allows the user to enter the size and then it will display solid square star pattern using while loop in C++ programming language
#include <iostream> #include <conio.h> using namespace std; int main() { int i,j,size; cout << "Please enter the size" << endl; cin>>size;//Takes input from the user for size i=1; while(i<=size){ j=1; while(j<=size){ cout<<"*"; j++; } cout<<"\n"; i++; } getch(); return 0; }
When the above code is executed it produces the following output
![C++ program to print solid square star pattern](https://i0.wp.com/code4javac.com/wp-content/uploads/2019/10/cppaquareforans.jpg?resize=260%2C217&ssl=1)
Approach
- The program requests to input for the “size of the pattern”
- The input stores in the variable “size”
- To iterate through the row, run the outer while loop from 1 to given size according to the loop structure while(i<=size)
- To iterate through the column, run the inner while loop from 1 to given size according to the loop structure while(j<=size);
- inside inner loop print “*”
- This activity continues until the condition of outer while loop becomes false
Program 3
C++ code to display square pattern Using do-while loop
This program allows the user to enter the size and then it will display solid square star pattern using do-while loop in C++ programming language
#include <iostream> #include <conio.h> using namespace std; int main() { int i,j,size; cout << "Please enter the size" << endl; cin>>size;//Takes input from the user for size i=1; do{ j=1; do{ cout<<"*"; j++; }while(j<=size); cout<<"\n"; i++; } while(i<=size); getch(); return 0; }
When the above code is executed it produces the following output
![C++ program to print solid square star pattern](https://i0.wp.com/code4javac.com/wp-content/uploads/2019/11/sqarestarpat3.jpg?resize=212%2C258&ssl=1)
Approach
- The program requests to input for the “size of the pattern”
- The input stores in the variable “size”
- To iterate through the row, run the outer do-while loop from 1 to given size according to the loop structure while(i<=size);
- To iterate through the column, run the inner do-while loop from 1 to given size according to the loop structure while(j<=size);
- inside inner loop print star
- This activity continues until the condition of outer while loop becomes false