Write a Python program to find the Least Common Multiple or LCM of two numbers using the While Loop, Functions, and Recursion.
In Mathematics, the Least Common Multiple of two or more integers is the smallest positive integer perfectly divisible by the given integer values without the remainder. For example, the LCM value of integers 2 and 3 is 12 because 12 is the smallest positive integer divisible by 2 and 3 (the remainder is 0).
Python Program to find the LCM of Two Numbers using a while loop
This Python program allows the user to enter two positive integer values. Within the Python while loop, we used the If statement to check whether the remainder of maximum % a and maximum % b equals zero. If true, Least Common Multiple = maximum; otherwise, skip that value.
# using If Else and While loop a = float(input(" Please Enter the First Value a: ")) b = float(input(" Please Enter the Second Value b: ")) if(a > b): maximum = a else: maximum = b while(True): if(maximum % a == 0 and maximum % b == 0): print("\n Least Common Multiple of {0} and {1} = {2}".format(a, b, maximum)) break; maximum = maximum + 1
LCM of Two Numbers Using Functions
This Python program is the same as above. However, we are separating the logic using the Functions.
def findLeastCM(a, b): if(a > b): maximum = a else: maximum = b while(True): if(maximum % a == 0 and maximum % b == 0): Val = maximum; break; maximum = maximum + 1 return Val num1 = float(input(" Please Enter the First : ")) num2 = float(input(" Please Enter the Second : ")) Val = findLeastCM(num1, num2) print("\n Least Common Multiple of {0} and {1} = {2}".format(num1, num2, Val))
Please Enter the First : 20
Please Enter the Second : 45
Least Common Multiple of 20.0 and 45.0 = 180.0
A better approach to writing the above program is to find the lcm of two numbers using functions.
def LeastCommonFactor(a, b): maximum = max(a,b) result = maximum while True: if result % a == 0 and result % b == 0: return result result += maximum a = int(input("Enter the First = ")) b = int(input("Enter the Second = ")) print("Least Common Multiple = ", LeastCommonFactor(a, b))
LCM of Two Numbers using GCD
This program finds the GCD of two numbers. Here, we are using the Temp variable to find it. Using this, we calculate LCM.
num1 = float(input(" Please Enter the First : ")) num2 = float(input(" Please Enter the Second : ")) a = num1 b = num2 while(num2 != 0): temp = num2 num2 = num1 % num2 num1 = temp hcf = num1 print("\n HCF of {0} and {1} = {2}".format(a, b, hcf)) Val = (a * b) / hcf print("\n Result of {0} and {1} = {2}".format(a, b, Val))
Python Program to find LCM of Two Numbers using Recursion
This program allows the user to enter two positive integer values and calculate the LCM of those two values by calling fdhcf function recursively.
def fdhcf(a, b): if(b == 0): return a; else: return fdhcf(b, a % b) num1 = float(input(" Please Enter the First Value : ")) num2 = float(input(" Please Enter the Second Value : ")) gcd = fdhcf(num1, num2) print("\n GCD of {0} and {1} = {2}".format(num1, num2, gcd)) res = (num1 * num2) / gcd print("\n LCM of {0} and {1} = {2}".format(num1, num2, res))