Python program to convert GCD of the given 2 numbers

Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to convert GCD of the given 2 numbers.

Here’s a simple Python program that finds the greatest common divisor (GCD) of two given numbers using the Euclidean algorithm:

Code for python program to convert GCD of the given 2 numbers

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

# Test the function
print(gcd(10, 20))

Output

10

Explanation

The program defines a function gcd that takes in two numbers as arguments (x, y). Inside the gcd function, it uses Euclidean algorithm which is implemented as a while loop.

Inside the loop, the values of x and y are swapped and updated each iteration with the remainder of the previous x divided by y , until y becomes zero. At that point, x contains the GCD of the two numbers.

You can test the function by calling it with any two numbers as arguments, as shown in the last line of the code. This will print out the greatest common divisor of the two numbers.

Python program to find LCM of the given 3 numbers

Conclusion

If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment