Wednesday, 25 December 2024

Day 58: Python Program to Find LCM of Two Numbers

 

import math

def find_lcm(num1, num2):

    """

    Function to find the LCM of two numbers.

    :param num1: First number

    :param num2: Second number

    :return: LCM of num1 and num2

    """

    gcd = math.gcd(num1, num2)

    lcm = (num1 * num2) 

    return lcm

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

result = find_lcm(num1, num2)

print(f"The LCM of {num1} and {num2} is: {result}")

#source code --> clcoding.com 

Code Explanation:

Importing the math Module
import math
The math module is imported to use its built-in math.gcd() function for calculating the Greatest Common Divisor (GCD) of two numbers.

Defining the Function: find_lcm
def find_lcm(num1, num2):
    """
    Function to find the LCM of two numbers.
    
    :param num1: First number
    :param num2: Second number
    :return: LCM of num1 and num2
    """
Purpose: Calculates the LCM of two numbers.
Parameters:
num1: First input number.
num2: Second input number.
Return Value: The LCM of num1 and num2.

Calculating the GCD
    gcd = math.gcd(num1, num2)
math.gcd(num1, num2):
Finds the Greatest Common Divisor (GCD) of num1 and num2.
The GCD is the largest integer that evenly divides both numbers.

Calculating the LCM
    lcm = (num1 * num2) 
​ 
The product of the two numbers is divided by their GCD to compute the LCM efficiently.
Why it works: This relationship between GCD and LCM ensures that the LCM is the smallest positive integer that is divisible by both numbers.

Returning the LCM
    return lcm
Returns the computed LCM to the caller.

User Input
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

Prompts the user to input two integers:
num1: The first number.
num2: The second number.
Converts the input strings to integers using int().

Calling the Function
result = find_lcm(num1, num2)
Calls the find_lcm() function with num1 and num2 as arguments.
Stores the result (the LCM) in the variable result.

Displaying the Result
print(f"The LCM of {num1} and {num2} is: {result}")
Uses an f-string to display the computed LCM in a user-friendly format.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (59) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (939) Python Coding Challenge (373) Python Quiz (31) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses