Ruby lcm() Method

Ruby lcm() Function with Examples

Ruby lcm() method calculates the LCM of two specified numbers. In mathematic LCM stands for least common multiple which means the lowest multiple divisible by both numbers.

This method is only applicable on integer data type only. Applying on the other data types including double/float will raise an error.

Ruby lcm() Method

Syntaxnum1.lcm(num2) or num2.lcm(num1)
Where num1 and num2 are the numbers for findings LCM of them.
ParametersThis method takes one of the two numbers as a parameter and another number directly works as an object.
Return valueReturns the LCM of the two specified numbers.

Example Program-1:

num1 = 12
num2 = 16
num3 = 8
num4 = 25

x = num1.lcm(num2)
y = num3.lcm(num4)
z = 15.lcm(10)

puts x
puts y
puts z

Output

48
200
30

Example Program-2:

num1 = 6
num2 = 14
num3 = 17
num4 = 20

puts "#{num1.lcm(num2)}"
puts "#{num3.lcm(num4)}"
puts "#{4.lcm(5)}"

Output

42
340
20
Posted in