Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Print a square pattern with # character

C++ For Loop: Exercise-17 with Solution

Write a program in C++ to print a square pattern with # character.

Pictorial Presentation:

C++ Exercises: Print a square pattern with # character

Sample Solution :-

C++ Code :

#include <iostream>
using namespace std;

int main()
{
    int size;
    cout << "\n\n Print a pattern like square with # character:\n";
    cout << "--------------------------------------------------\n";
    cout << " Input the number of characters for a side: ";
    cin >> size;
    for (int row = 1; row <= size; ++row) 
    {
        for (int col = 1; col <= size; ++col) 
        {
            cout << "# ";
        }
        cout << endl;
    }
    return 0;
}

Sample Output:

 Print a pattern like square with # character:                         
--------------------------------------------------                     
 Input the number of characters for a side: 4                          
# # # #                                                                
# # # #                                                                
# # # #                                                                
# # # #

Flowchart:

Flowchart: Print a square pattern with # character

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to list non-prime numbers from 1 to an upperbound.
Next: Write a program in C++ to display the cube of the number upto given an integer.

What is the difficulty level of this exercise?