MC logo

Basic I/O II

  Ruby Example Code

<<Basic I/O I io3.rb File I/O>>
print "Triangle height: "
h = gets.to_f;
print "Triangle width: "
w = gets.to_f;
area = 0.5*h*w
print "Triangle height ", h, " width ", w, " has area ", area, "\n"
See:Ruby Manual [1][2]

Similar to the last one, the to_f method of class String converts the input string to a floating-point number.
<<Basic I/O I File I/O>>