Robotics & Artificial Intelligence
Write a user-defined function def HCF() to input two numbers from the user. The program calculates greatest common divisor of the two numbers inside the function. Finally, it returns the value to main program to display the result.
Sample Input: First Number: 25
Second Number: 60
HCF = 5
Answer
def HCF(a, b):
if(a < b):
small = a
else:
small = b
for i in range(1, small + 1):
if(a % i == 0 and b % i == 0):
hcf = i
return(hcf)
# main program
num1 = int(input("Enter first number:"))
num2 = int(input("Enter second number:"))
result = HCF(num1, num2)
print("HCF =", result)Output
Enter first number:25
Enter second number:60
HCF = 5
Related Questions
Write a user-defined function
def Sum_Digits()to input a number from the user. The program calculates sum of the digits of the number inside the function without returning any data to the main program.Sample Input: 345
Output: 3 + 4 + 5 = 12
Write a user-defined function
def Rev_Digits()to input a number from the user. The program reverses the digits to produce a new number inside the function. However, it displays the new number inside the function without returning any data to the main program.Sample Input: 257
Output: 752
Write a user-defined function
def Fact()to input 1 to 5 from the user. The program calculates the factorial of each number inside the function. Finally, it returns the values to main program to display the result.Hint:
def Fact(n): f = 1 for a in range(1, n+1): f = f * a return f # main program for i in range(1, 6): print("Factorial of", i, "=", Fact(i))Define function.