In this python basic program, we will find the HCF of two numbers defined by users.
Steps to solve the program
- Take two numbers as input through the user.
- Using an if-else statement check which number is smaller.
- Use for loop with the range function to iterate over numbers from 1 to the smaller number.
- During iteration check whether the iterated number divides both input numbers, if yes then assign that number to the variable.
- After the loop is finished print the output.
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
if num1 > num2:
smaller = num2
else:
smaller = num1
for i in range(1, smaller+1):
if((num1 % i == 0) and (num2 % i == 0)):
hcf = i
print(f"H.C.F of {num1} and {num2}: {hcf}")
Output :
Enter number 1: 150
Enter number 2: 200
H.C.F of 150 and 200: 50