Logic Building
def gcd(a, b):
# Base case
if b == 0:
return a
# Recursive case (Euclidean algorithm)
return gcd(b, a % b)
# Test
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
result = gcd(abs(a), abs(b))
print(f"GCD: {result}")Output
Enter first number: 48 Enter second number: 18 GCD: 6
Euclidean algorithm: GCD(a,b) = GCD(b, a%b).
Key Concepts:
- Base case: b == 0, answer is a
- Recursive: GCD(b, a % b)
- Continues until remainder is 0