Square and Hollow Square in C - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Square and Hollow Square in C

TimCerejaTimCereja Posts: 17Member
Question about this C program that I've written. The objective is to print a square and a hollow square to the size input by the user. I've got the solid square down fine, but I'm having a bit of trouble with the hollow one. I understand that something must be put in between the two loops, but I'm having difficulty finding the solution. Any suggestions for this source code? Thanks a lot ...

#include

int main( void )
{
int row;
int col;
int num;

printf( "Please Enter a Number Between 1 and 20: ")
scanf( "%d", &num );
printf( "
" );

for(row = 0; row < num; row++)
{
for(col = 0; col < num; col++)
{
printf("*");
}
printf("
");
}

return 0;
}

Comments

  • TimCerejaTimCereja Posts: 17Member
    Well nevermind ... I put up this post prematurely. After some thinking about the problem I figured it out. Here it is:

    #include
    int main( void )
    {
    int row;
    int col;
    int num;

    printf("
    ");
    printf( "Please Enter a Number Between 1 and 20: " );
    scanf( "%d", &num );

    printf( "
    " );

    for(row = 0; row < num; row++)
    {
    for(col = 0; col < num; col++)
    {
    printf("*");
    }
    printf("
    ");
    }

    printf("
    ");

    for(col = 0; col < num; col++)
    {
    printf("*");
    }

    printf("
    ");

    for(row = 0; row < (num - 2); row++)
    {
    printf("*");

    for(col = 2; col <= num; col++)
    {
    if (col == num)
    printf("*");
    else
    printf(" ");
    }
    printf("
    ");
    }

    for(col = 0; col < num; col++)
    {
    printf("*");
    }

    printf("
    ");

    return 0;
    }

Sign In or Register to comment.