Exercise 2: Finding Modulo
Write code to take modulo of two numbers.
We'll cover the following
Modulo operator
In Ruby, the %
symbol represents the modulo operator. The modulo of two positive numbers is the remainder we get if we divide the first number by the second number. For example, the code below prints 1
.
puts 5 % 2
Note: Beware, though, if we include negative numbers. In that case, there may be a slight difference in the way we’d expect this remainder to be defined. Different languages use different conventions for this operator, and the distinction can be confusing for a beginner.
The modulo operator is convenient when determining if a number is divisible by another because it’d produce a remainder of zero!
Problem statement
In this problem, calculate the modulo of the two numbers, num1
and num2
, and store the computed value in the result
.
Tip: Insert your code in line 4.
Try it yourself
def calculate_mod(num1, num2)result =-99# Start your code herereturn resultend