The next sample exam problem is about using nested loops and calculations to print on the console a diamond of given size, like the ones shown below in the examples.
Watch the video lesson about solving the "Perfect Diamond" problem: https://youtu.be/UqQFpM_cgbY.
Write a program that reads an integer n from the console and draws a perfect diamond with size n as in the examples below.
The input is an integer n within the range [1 … 1000].
The diamond should be printed on the console as in the examples below.
In tasks for drawing figures, the most important thing to consider is the sequence in which we will draw. Which items are repeated and with what steps? We can clearly see that the top and bottom parts of the diamond are the same. The easiest way to solve the problem is by creating a loop that draws the upper part, and then another one loop that draws the bottom part (opposite to the top one).
We will read the number n from the console.
We start painting the top half of the diamond. We clearly see that each row starts with a few empty spaces and
*. If we take a closer look, we will notice that the empty spaces are always equal to
n – the number of lines (the first row is n-1, the second – n-2, etc.). We will start by drawing the number of empty spaces, and the first asterisk. Let's not forget to use
Console.Write(…) instead of
Console.WriteLine(…) to stay on the same line. At the end of the line we write
Console.WriteLine(…) to go to a new line. Notice that we start counting from 1, not from 0. Next, we will only add a few times
-* to finish the line.
Here is part of the code for the top of the diamond:
What remains is to complete each line with the required number of
-* elements. On each row we have to add
i-1 such items (on the first 1-1 -> 0, the second -> 1, etc.)
Here is the complete code for drawing the top of the diamond:
To draw the bottom part of the diamond, we have to reverse the upper part. We will count from
n-1, because if we start from
n, we will draw the middle row twice. Do not forget to change the step from
Here is the code for drawing the bottom part of the diamond:
What remains is to assemble the whole program by first reading the input, printing the top part of the diamond and then the bottom part of the diamond.
Test your solution here: https://judge.softuni.org/Contests/Practice/Index/516#8.