Q:

Ruby program to find the LCM of two given numbers using recursion

0

In this program, we will read two integer numbers from the user and find the LCM of input numbers using recursion.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to find the LCM of two given numbers using recursion is given below. The given program is compiled and executed successfully.

# Ruby program to find the LCM of 
# two given numbers using recursion

def LCM(a, b, res)
    if (res % a == 0 && res % b == 0)
        return res;
    else
        LCM(a, b,res + 1);
    end
end

print "Enter number1: ";
number1 = gets.chomp.to_i;  

print "Enter number2: ";
number2 = gets.chomp.to_i;  

result = LCM(number1, number2,1);

print "LCM is: ",result;

Output:

Enter number1: 45
Enter number2: 75
LCM is: 225

Explanation:

In the above program, we read two integer numbers from the user. Then we found the LCM of input numbers using recursive function LCM(). Then we printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now