Ruby - arithmetic operators example
The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/), modulo(%), and exponent(**) operators.
a = 9.0 b = 2.0 puts "a = #{a}, b = #{b} \n\n" #Add a and b result_add = a + b puts "a + b = #{result_add}" #Subtract b from a result_sub = a - b puts "a - b = #{result_sub}" #Multiply a and b result_mul = a * b puts "a * b = #{result_mul}" #Divide a by b result_div = a / b puts "a / b = #{result_div}" #return division remainder result_modulo = a % b puts "a % b = #{result_modulo}" #a raised to the power of b result_pow = a ** b puts "a ** b = #{result_pow}"
The output of the above code will be:
a = 9.0, b = 2.0 a + b = 11.0 a - b = 7.0 a * b = 18.0 a / b = 4.5 a % b = 1.0 a ** b = 81.0
❮ Ruby - Operators