Search⌘ K
AI Features

Print Out a Half Diamond

Explore how to print a half diamond shape in Ruby by applying loops and conditional statements. Learn to manage loop decrements effectively to control the number of hash characters per row, understanding how to use if-else conditions to shape the output. This lesson builds foundational skills in flow control and pattern printing.

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  ...