Learn how to print only the border of a square (hollow box) with diagonals using asterisks in the C programming language.

How to print a hollow square/box/rectangle pattern with diagonals using asterisks in the C language

When i needed to do this on the university in introduction to programming, achieving this task gave me a couple of extra points for the final note of the work of Loops. In this article, we'll share with you a very simple script to create a hollow box/square output according to X number of asterisks with a diagonal on the center on every side in the console with the C programming language:


void main()
    int number;

    printf("Provide the number of asterisks by side: \n");

    scanf("%d", &number);

    int i, j;

    for (i = 1; i <= number; i++)
        for (j = 1; j <= number; j++)
                i == 1 || i == number ||
                j == 1 || j == number ||
                i == j || j == (number - i + 1)
                printf(" ");


Happy coding !

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.