Arithmetic operators help us do math with numbers in programs. They let us add, subtract, multiply, and divide easily.
Arithmetic operators in Ruby
Introduction
Syntax
Ruby
result = number1 + number2 result = number1 - number2 result = number1 * number2 result = number1 / number2 result = number1 % number2
Use + for addition, - for subtraction, * for multiplication, / for division, and % for remainder (modulus).
Division with / returns a float if any number is a float, otherwise an integer.
Examples
Ruby
sum = 5 + 3 puts sum
Ruby
difference = 10 - 4 puts difference
Ruby
product = 7 * 6 puts product
Ruby
quotient = 20 / 4 puts quotient
Ruby
remainder = 17 % 5 puts remainder
Sample Program
This program asks for two numbers, then shows the result of adding, subtracting, multiplying, dividing, and finding the remainder.
Ruby
puts "Enter first number:" num1 = gets.chomp.to_i puts "Enter second number:" num2 = gets.chomp.to_i sum = num1 + num2 difference = num1 - num2 product = num1 * num2 quotient = num1 / num2 remainder = num1 % num2 puts "Sum: #{sum}" puts "Difference: #{difference}" puts "Product: #{product}" puts "Quotient: #{quotient}" puts "Remainder: #{remainder}"
Important Notes
Division by zero will cause an error, so avoid dividing by zero.
Using to_i converts input to whole numbers; for decimals use to_f.
Summary
Arithmetic operators let you do basic math in Ruby.
Use +, -, *, /, and % for addition, subtraction, multiplication, division, and remainder.
Remember to handle division carefully to avoid errors.
Practice
1. Which of the following is the correct symbol for multiplication in Ruby?
easy
Solution
Step 1: Recall basic arithmetic symbols in Ruby
Ruby uses standard arithmetic symbols: + for addition, - for subtraction, * for multiplication, / for division.Step 2: Identify the multiplication symbol
The symbol for multiplication is the asterisk (*).Final Answer:
* -> Option BQuick Check:
Multiplication = * [OK]
Hint: Remember * means multiply in most languages including Ruby [OK]
Common Mistakes:
- Confusing + with multiplication
- Using / instead of *
- Using - instead of *
2. Which of the following Ruby expressions correctly calculates the remainder of 10 divided by 3?
easy
Solution
Step 1: Understand the modulus operator
The modulus operator (%) returns the remainder after division.Step 2: Identify the correct expression for remainder
10 % 3 calculates the remainder when 10 is divided by 3, which is 1.Final Answer:
10 % 3 -> Option DQuick Check:
Remainder = % [OK]
Hint: Use % to get remainder after division [OK]
Common Mistakes:
- Using / instead of %
- Using * or - which do not give remainder
- Confusing % with division
3. What is the output of the following Ruby code?
result = 15 / 4 puts result
medium
Solution
Step 1: Understand integer division in Ruby
When dividing two integers, Ruby performs integer division, discarding the decimal part.Step 2: Calculate 15 divided by 4 as integer division
15 / 4 equals 3.75, but integer division returns 3.Final Answer:
3 -> Option AQuick Check:
Integer division truncates decimals = 3 [OK]
Hint: Integer division drops decimals unless floats used [OK]
Common Mistakes:
- Expecting float result without floats
- Confusing / with %
- Thinking output is 4 or 3.75
4. Identify the error in this Ruby code snippet:
num = 10 result = num / 0 puts result
medium
Solution
Step 1: Analyze the division operation
The code divides 10 by 0, which is mathematically undefined.Step 2: Understand Ruby's behavior on division by zero
Ruby raises a ZeroDivisionError when dividing by zero.Final Answer:
Division by zero error -> Option AQuick Check:
Dividing by zero causes error [OK]
Hint: Never divide by zero; it causes runtime error [OK]
Common Mistakes:
- Thinking output is 0
- Assuming syntax error
- Ignoring runtime exceptions
5. You want to calculate the average of three numbers: 7, 8, and 9 in Ruby. Which expression correctly computes the average as a float?
hard
Solution
Step 1: Calculate sum of numbers
Sum is 7 + 8 + 9 = 24.Step 2: Divide by 3 as float division
Dividing by 3.0 forces float division, giving 8.0 instead of integer 8.Final Answer:
(7 + 8 + 9) / 3.0 -> Option CQuick Check:
Use float divisor for float average [OK]
Hint: Divide by float (3.0) to get decimal average [OK]
Common Mistakes:
- Using integer division losing decimals
- Misusing % instead of /
- Wrong operator precedence without parentheses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
