Let's take a look at things.
You know that it's going to be a square, so...
Input:
Output:
* * * * * *
* *
* *
* *
* *
* * * * * *
|
From the output, you know that only two lines will have N number of characters
on the square: the top and bottom of the square. The rest will just have two
characters on the sides. (see below)
* * * * * * <- a full line of N characters
* * <- two characters on side
* * <- two characters on side
* * <- two characters on side
* * <- two characters on side
* * * * * * <- a full line of N characters
|
By knowing that you have to print two lines, you can then use a loop to print
the "hollow" part of the square. And you already know how many "hollow" lines
to print: it is num - 2, because we take two off to account for the top and
bottom sides of square that will be printed.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int num, x = 0;
...
if(num % 2 == 0) // accepting only even-numbered squares
{
// First line, we will print a full line of N characters
// The middle area here, we will print the hollowed area of the square
// Last line is the same as the first, we will print a full line of N
// characters, thus closing the square
}
|
It's probably easier to use a for loop than a while loop for this sort of
thing. Or at least my experiences.