The gcd() function in Ruby returns the gcd of two numbers. GCD signifies the greatest common divisor which divides both the numbers.
Syntax: number1.gcd(number2)
Parameter: The function requires two numbers whose gcd is to be returned.
Return Value: The function returns the gcd of two numbers
Example #1:
# Ruby program of Integer gcd() function # Initializing the numbers num1 = 10 num2 = 15 num3 = 21 num4 = 14 # Prints the gcd value puts num1.gcd(num2)
puts num3.gcd(num4)
|
Output:
5 7
Example #2:
# Ruby program of Integer gcd() function # Initializing the numbers num1 = 22
num2 = 18
num3 = 30
num4 = 25
# Prints the gcd value puts num1.gcd(num2) puts num3.gcd(num4) |
Output:
2 5