Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Functions

Function to find GCD using Euclidean method - Python Program

Example 1 :

def gcd(a, b): """Return GCD of two numbers.""" while b: a, b = b, a % b return a print(gcd(48, 18))

Output

 
OUTPUT  :
6

Example 2 :

def gcd_euclidean(a, b): """ Calculates the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Args: a: The first integer. b: The second integer. Returns: The GCD of a and b. """ if b == 0: return a else: return gcd_euclidean(b, a % b) # Example usage num1 = 48 num2 = 18 result = gcd_euclidean(num1, num2) print(f"The GCD of {num1} and {num2} is: {result}")

Output

 
OUTPUT  :
The GCD of 48 and 18 is: 6