Lesson 4 of 15

Numbers

Numbers in Ruby

Ruby has two main numeric types: Integer and Float.

Integer Arithmetic

puts 2 + 3    # 5
puts 10 - 4   # 6
puts 3 * 7    # 21
puts 2 ** 8   # 256  (exponentiation)
puts 17 / 5   # 3    (integer division — truncates)
puts 17 % 5   # 2    (remainder / modulo)

Integer division truncates toward zero — 17 / 5 is 3, not 3.4.

Float Arithmetic

Use a float operand to get a float result:

puts 10.0 / 4   # 2.5
puts 1.5 + 2.5  # 4.0

Useful Methods

puts -7.abs      # 7   (absolute value — careful: -7.abs is -(7.abs) = -7!)
puts (-7).abs    # 7   (correct)
puts 3.14.round  # 3
puts 3.14.ceil   # 4
puts 3.14.floor  # 3

Your Task

Print the results of:

  1. 2 ** 10
  2. 10 / 3
  3. 10.0 / 4
  4. 17 % 5
ruby.wasm loading...
Loading...
Click "Run" to execute your code.