Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.