How to print a hollow square/box/rectangle pattern with diagonals using asterisks in the C 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:

#include<stdio.h>

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++)
        {
            if
            (
                i == 1 || i == number ||
                j == 1 || j == number ||
                i == j || j == (number - i + 1)
            ){
                printf("*");
            }else{
                printf(" ");
            }
        }

        printf("\n");
    }
}

Happy coding !

This could interest you

Become a more social person