This is a Python Program to find the LCM of two numbers using recursion.
The program takes two numbers and finds the LCM of two numbers using recursion.
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.
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)
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.
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.
- Check Information Technology Books
- Apply for Python Internship
- Practice Programming MCQs
- Apply for Programming Internship
- Check Python Books