Print Out a Half Diamond
Let's learn about comparison operators and control flow statements, and then use them to create the shape of a half diamond.
Problem
Write a program that prints out half of the diamond shape using hash characters, as shown below:
################################################################
Print half diamond shape
Purpose
Learn decrement count in loops.
Analyze
The key to this problem is to determine the number of hashes for the corresponding rows.
Rows 1–8: Same as the row number
Rows 9–15: Same as row number subtracted from 16.
Row | Hashes |
1 | 1 |
2 | 2 |
3 | 3 |
... | ... |
8 | 8 |
9 | 16 - 9 = 7 |
10 | 16 - 10 = 6 |
11 | 16 - 11 = 5 |
... | ... |
15 | 16 - 15 = 1 |
Hints
Control flows using if-else
Code, in its simplest form, is executed from top to bottom. But if there are ...