Solution: Finding the HCF
Go over the implementation of finding the HCF of two numbers.
We'll cover the following...
We'll cover the following...
Solution
divisors_list_1 = []
divisors_list_2 = []
puts "Enter first number: "
num1 = gets.chomp.to_i
(1..num1).each do |x|
check = num1 % x
if check == 0
divisors_list_1 << x
end
end
puts "Enter second number: "
num2 = gets.chomp.to_i
(1..num2).each do |x|
check = num2 % x
divisors_list_2 << x if check == 0
end
d1sorted = divisors_list_1.sort.reverse
d1sorted.each do |elem|
if divisors_list_2.include?(elem)
puts "The HCF is #{elem}"
break
end
endCalculating HCF of two numbers
Explanation
Lines 5–10: We find all the divisors for the first input number.
Lines 13–16: We find all the divisors for the second ...