HCFWrite a program to find out the Highest Common Factor for given two numbers.
Question
HCF
Write a program to find out the Highest Common Factor for given two numbers.
Solution
Sure, here is a simple Python program that calculates the Highest Common Factor (HCF) of two numbers:
def compute_hcf(x, y):
# Choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The H.C.F. is", compute_hcf(num1, num2))
This program works by using a function compute_hcf(x, y)
that takes two numbers as input. It then determines the smaller of the two numbers and uses a for loop to iterate from 1 to that smaller number. In each iteration, it checks if both numbers are divisible by the current number i
. If they are, it assigns i
to hcf
. The function finally returns hcf
which is the highest common factor of the two numbers.
The program then asks the user to input two numbers and prints out their H.C.F.
Similar Questions
p 2 3 53 2# #= q 2 3 52# #=(a) Find the highest common factor (HCF) of p and q.
The product of the LCM and the HCF of two numbers is 24. If the difference of the numbers is 2, then the greater of the number isa.8b.6c.3d.4
The product of 2 numbers is 6750 and their LCM is 450. If difference between the numbers is equal to their HCF, then find the smaller number
If a, b are two distinct prime number than a highest common factor of a, b is ___________ 201ab
Write a program to count the number of prime factors of a given positive integer and calculate the difference between the number and the lowest prime factor.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.