To print a pyramid pattern in Ruby, you can use nested loops where the outer loop controls the rows and the inner loops control the spaces and stars printed in each row.
In this example,
print_pyramid
that takes an integer n
as input to represent the number of rows in the pyramid.puts
to move to the next line.def print_pyramid(n)
n.times do |i|
print ' ' * (n - i - 1)
puts '*' * (2 * i + 1)
end
end
print_pyramid(5)
* *** ***** ******* *********
In this tutorial, we learned How to print Pyramid Pattern in Ruby language with well detailed examples.