Ruby’s modulo()
function returns the remainder of two given numbers that are divided.
Figure 1, below, shows the visual representation of the modulo()
function.
num_1.modulo(num_2)
# where num_1 and num_2 are the numbers whose modulo is to be calculated
# num_1 % num_2
This function requires another number, (num_2)
, as a parameter to calculate the modulo
.
This function returns the remainder after the two given numbers are divided.
The following example shows how to use modulo()
function in Ruby.
#Both positive numbersprint "(13).modulo(2) : ",(13).modulo(2) , "\n"#one negative and another positiveprint "(16).modulo(-3) : ",(16).modulo(-3) , "\n"print "(21).modulo(-4) : ",(21).modulo(-4) , "\n"#fractional numberprint "(22.5).modulo(1.6) : ",(22.5).modulo(1.6) , "\n"