We read n from the console and we write it down in a variable of type
We start to draw the upper part of the diamond. The first thing we need to do is to calculate the number of the outer dashes
leftRight (the dashes on the outer side of the stars). It is equal to
(n - 1) / 2, rounded down.
After we have calculated
leftRight, we start to draw the upper part of the diamond. We can start by making a loop from
n / 2 + 1 (rounded down).
At each iteration of the loop the following steps must be taken:
- We draw on the console the left dashes (with length
leftRight) and right after them the first star.
- We will calculate the distance between the two stars. We can do this by subtracting from n the number of the outer dashes, and the number 2 (the number of the stars, i.e. the diamonds outline). The result of the subtraction we write down in a variable
midis lower than 0, we know that on the row there should be only 1 star. If it is higher or equal to 0 then we have to print dashes with length
midand one star after them.
- We draw on the console the right outer dashes with length
- In the end of the loop we decrease
leftRightby 1 (the stars are moving away from each other).
We are ready with the upper part.