c++ - print a filled square in console

C++ - print a filled square in console

To print a filled square in the console using C++, you can use nested loops to iterate through the rows and columns of the square. Here's a simple example to demonstrate how to do this:

Example Code

#include <iostream> int main() { int size; // Get the size of the square from the user std::cout << "Enter the size of the square: "; std::cin >> size; // Print the filled square for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << '*'; // Print the character to fill the square } std::cout << std::endl; // Move to the next line after finishing a row } return 0; } 

Explanation

  1. Include Header:

    • #include <iostream>: Includes the input/output stream library for console operations.
  2. Main Function:

    • int main() { ... }: Entry point of the C++ program.
  3. Get the Size:

    • std::cout << "Enter the size of the square: ";: Prompt the user to enter the size of the square.
    • std::cin >> size;: Read the size from user input.
  4. Nested Loops to Print the Square:

    • Outer loop (for (int i = 0; i < size; ++i)): Iterates over each row.
    • Inner loop (for (int j = 0; j < size; ++j)): Iterates over each column within a row.
    • std::cout << '*';: Prints the character (here, *) to fill the square.
    • std::cout << std::endl;: Moves to the next line after finishing printing one row.

Example Output

If the user inputs 5, the output will be:

***** ***** ***** ***** ***** 

You can modify the character (*) to any other character or symbol depending on your needs, and the size of the square will adjust based on the user's input.

Examples

  1. How to print a filled square in console using C++?

    Description: Print a filled square of a given size using nested loops.

    Code:

    #include <iostream> int main() { int size = 5; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << "* "; } std::cout << std::endl; } return 0; } 
  2. How to print a filled square of given character in console using C++?

    Description: Print a filled square using a specified character.

    Code:

    #include <iostream> int main() { int size = 5; char fillChar = '#'; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << fillChar << ' '; } std::cout << std::endl; } return 0; } 
  3. How to print a filled square in console using C++ with user input?

    Description: Print a filled square of a size specified by the user.

    Code:

    #include <iostream> int main() { int size; std::cout << "Enter the size of the square: "; std::cin >> size; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << "* "; } std::cout << std::endl; } return 0; } 
  4. How to print a filled square of variable size in console using C++?

    Description: Print a filled square with a size that can be easily changed by modifying a variable.

    Code:

    #include <iostream> int main() { int size = 7; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << "* "; } std::cout << std::endl; } return 0; } 
  5. How to print a filled square with a border in console using C++?

    Description: Print a filled square with a different border character.

    Code:

    #include <iostream> int main() { int size = 5; char fillChar = '*'; char borderChar = '#'; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { if (i == 0 || i == size - 1 || j == 0 || j == size - 1) { std::cout << borderChar << ' '; } else { std::cout << fillChar << ' '; } } std::cout << std::endl; } return 0; } 
  6. How to print a filled square with spaces between characters in console using C++?

    Description: Print a filled square with spaces between characters for better readability.

    Code:

    #include <iostream> int main() { int size = 5; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << "* "; } std::cout << std::endl; } return 0; } 
  7. How to print a filled square of numbers in console using C++?

    Description: Print a filled square where each cell contains its row number.

    Code:

    #include <iostream> int main() { int size = 5; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << i << ' '; } std::cout << std::endl; } return 0; } 
  8. How to print a filled square with increasing numbers in console using C++?

    Description: Print a filled square where each cell contains an incrementing number.

    Code:

    #include <iostream> int main() { int size = 5; int num = 1; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << num++ << ' '; } std::cout << std::endl; } return 0; } 
  9. How to print a filled square with alternating characters in console using C++?

    Description: Print a filled square with alternating characters.

    Code:

    #include <iostream> int main() { int size = 5; char char1 = '*'; char char2 = '+'; for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << ((i + j) % 2 == 0 ? char1 : char2) << ' '; } std::cout << std::endl; } return 0; } 
  10. How to print a filled square of given size and character in console using C++ with functions?

    Description: Create a function to print a filled square, allowing reusability with different sizes and characters.

    Code:

    #include <iostream> void printSquare(int size, char fillChar) { for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) { std::cout << fillChar << ' '; } std::cout << std::endl; } } int main() { int size = 5; char fillChar = '@'; printSquare(size, fillChar); return 0; } 

More Tags

ubuntu-16.04 minikube laravel-middleware subnet fixed data-science photo android-shapedrawable urlopen assistant

More Programming Questions

More Date and Time Calculators

More Genetics Calculators

More Transportation Calculators

More Weather Calculators