Write a simple python function to GCD of two integers inputted by the user.
# The required output is as:
please input the first number
please input the second number
Answers
Answer:
In Python, the math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments.
Answer:
The output of the program:
Please Enter the First : 75
Please Enter the Second : 255
HCF of 75.0 and 255.0 = 15.0
Explanation:
In this program, we are finding the GCD without using the Temp variable.
- Greatest Common Divisor (GCD) is a mathematical term to find the greatest common factor that can perfectly divide the two numbers.
- A GCD is also known as the Highest Common Factor (HCF). For example, the HCF/ GCD of two numbers 54 and 24 is 6. Because 6 is the largest common divisor that completely divides 54 and 24.
Program
num1 = float(input(" Please Enter the First : "))
num2 = float(input(" Please Enter the Second : "))
a = num1
b = num2
if(num1 == 0):
print("\n HCF of {0} and {1} = {2}".format(a, b, b))
while(num2 != 0):
if(num1 > num2):
num1 = num1 - num2
else:
num2 = num2 - num1
hcf = num1
print("\n HCF of {0} and {1} = {2}".format(a, b, hcf))