Q:

C program to print hollow square of stars

belongs to collection: C Programs to print Series

0

In this exercise, I will show you, How to write a C program to print hollow square star pattern. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here, I have mentioned two methods to print hollow square star pattern.

Method1:

#include <stdio.h>
int main()
{
    int x = 0,y = 0;
    unsigned int squareSide = 0;
    printf("Enter Side of a Square = ");
    scanf("%u",&squareSide);
    for(x=1; x<=squareSide; ++x)
    {
        for(y=1; y<=squareSide; ++y)
        {
            if((x==1) || (x==squareSide) || (y==1) || (y==squareSide))
            {
                //Print star
                printf("*");
            }
            else
            {
                //Print space
                printf(" ");
            }
        }
        // Print new line
        printf("\n");
    }
    return 0;
}

Output:

Enter Side of a Square = 10

**********

*            *

*            *

*            *

*            *

*            *

*            *

*            *

*            *

**********

----------------------------------------------------------------

Method2:

#include <stdio.h>
#define isBorder(x,y,n) ((x==1) || (x==n) || (y==1) || (y==n))
int main()
{
    int x = 0,y = 0;
    unsigned int squareSide = 0;
    printf("Enter Side of a Square = ");
    scanf("%u",&squareSide);
    for(x=1; x<=squareSide; ++x)
    {
        for(y=1; y<=squareSide; ++y)
        {
            isBorder(x,y,squareSide)? printf("*"):printf(" ");
        }
        // Print new line
        printf("\n");
    }
    return 0;
}

Output:

Enter Side of a Square = 10

**********

*            *

*            *

*            *

*            *

*            *

*            *

*            *

*            *

**********

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programs to print Series

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to print hollow square star pattern with... >>
<< C program to print any character in a square patte...