Print Square of Asterisks - C++ Statement

C++ examples for Statement:for

Description

Print Square of Asterisks

Demo Code

#include <iostream>

int main(int argc, const char *argv[]) {
    int count = 0;

    while (true) {
        std::cout << "Enter a number between 1 and 20: ";
        std::cin >> count;//  www  .j a v a2s .com

        if (count > 0 && count <= 20)
            break;
    }

    for (int i = 0; i < count; i++) {
        if (count > 1)
            printf("*");

        for (int j = 1; j < count - 1; j++) {
            if (i == 0 || i == count - 1)
                printf("*");
            else
                printf(" ");
        }
        printf("*\n");
    }
    return 0;
}

Result


Related Tutorials