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))
The program defines a function
gcd that takes in two numbers as arguments (
y). Inside the
gcd function, it uses Euclidean algorithm which is implemented as a while loop.
Inside the loop, the values of
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.
If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.