Sunday, 10 August 2025

LCM of two numbers without built-in functions

LCM of Two Numbers

LCM (Least Common Multiple) of two given numbers without using built-in math functions or user-defined functions.

# LCM of two numbers without use of built-in functions.

# Input two numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# Store the maximum of num1 and num2
if num1 > num2:
    max_num = num1
else:
    max_num = num2

# Find LCM using loop
while True:
    if (max_num % num1 == 0) and (max_num % num2 == 0):
        lcm = max_num
        break
    max_num += 1

# Display the result
print("LCM of", num1, "and", num2, "is", lcm)

Output of program

Enter first number: 5

Enter second number: 7

LCM of 5 and 7 is 35


No comments:

Post a Comment