Python Program to Find the LCM of Two Numbers using Recursion

This is a Python Program to find the LCM of two numbers using recursion.

Problem Description

The program takes two numbers and finds the LCM of two numbers using recursion.

Problem Solution

1. Take two numbers from the user.
2. Initialize the multiple variable with the maximum value among two given numbers.
3. Check whether the multiple variable clearly divides both the number or not.
4. If it does, then end the process and return the multiple as the LCM.
5. If multiple doesn’t clearly divides both given numbers then increment the multiple by the max values among both the given numbers.
5. Return the multiple variable and print the LCM of the two variables.
6. Exit.

Program/Source Code

Here is source code of the Python Program to find the LCM of two numbers using recursion. The program output is also shown below.

def lcm(a,b):
    lcm.multiple=lcm.multiple+b
    if((lcm.multiple % a == 0) and (lcm.multiple % b == 0)):
        return lcm.multiple;
    else:
        lcm(a, b)
    return lcm.multiple
lcm.multiple=0
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
if(a>b):
    LCM=lcm(b,a)
else:
    LCM=lcm(a,b)
print(LCM)
Program Explanation

1. User must take two numbers from the user.
2. The multiple variable are initialized with the maximum value among two given numbers.
3. The multiple variable is checked if clearly divides both the numbers or not.
4. If it does, then the process is ended and the multiple is returned as the LCM.
5. If multiple doesn’t clearly divides both given numbers then the multiple variable is returned by the max values among both the given numbers.
5. The multiple variable is returned and the LCM of the two variables is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter first number:126
Enter second number:12
LCM is: 
252
 
Case 2:
Enter first number:25
Enter second number:5
LCM is: 
25

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.