Hackety Hacking Problem Pyramid NestedLoops

From OLPC
Revision as of 22:21, 16 October 2007 by Ankur.verma (talk | contribs) (New page: == Problem Statement == Pyramid-shaped structures were built by many ancient civilizations. Here is then a problem for you? You have to design a two dimensional view of the pyramid. HIN...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Problem Statement

Pyramid-shaped structures were built by many ancient civilizations. Here is then a problem for you?

You have to design a two dimensional view of the pyramid.

HINT: Use Nested For loops

Sample Output

Enter the height of the pyramid: 4
   *
  * *
 * * *
* * * *

Curriculum Connections

This problem gives student help in understanding things mentioned below:

  • How to use nested loops.

Solution in Ruby

print("Enter the values of lines to be printed")
N=gets.to_i;
print("\n")
0.upto(N-1) do | i | 
	
	0.upto(N-1-i) do | k |
	print " "
	end
	
print "*"
	
	0.upto(i-1) do | j |
	print(" *")
	end

print("\n")
end