GCD and LCM in Python

GCD and LCM in Python

The logic to find the GCD and LCM is quite simple. First write a function to get GCD and then write function to get LCM of the number. GCD can get calculated by the logic to find reminder after dividing one number by another, next time dividing the divisor by reminder left out. Continue the process till divisor is greater then zero. Now to get LCM we need to apply logic to divide the multiplication of numbers by the GCD value of numbers. Like if a and b are two numbers then the LCM will (aXb)/gcd(a,b). Lets look into the program.

GCD and LCM calculation in Python.

Output will be like this –

Enter first number – 4
Enter second number – 12
GCD of 4 and 12 is = 4
LCM of 4 and 12 is = 12.0