HCFWrite a program to find out the Highest Common Factor for given two numbers.
Question
HCFWrite 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
HCFWrite a program to find out the Highest Common Factor for given two numbers.Constraints:NAExample:Sample Input:12 30Sample Output:6Explanation:Highest common factor for 12 and 30 is 6 . So the output is 6
Write a program to find out the Highest Common Factor for given two numbers.
Find the Highest Common Factor (HCF) of 147 and 70.
Find the highest common factor (HCF) of 32 and 120.
Single File Programming QuestionProblem StatementSam is on a quest to decipher enchanted scrolls in an ancient temple that requires unveiling the HCF (Greatest Common Divisor) and LCM (Least Common Multiple) of two numbers. Write a program where Sam enters two positive integers, calculates the HCF and LCM using a for loop, and displays them.ExampleInput:6 3Output:HCF = 3LCM = 6Explanation:HCF (Greatest Common Divisor) of two numbers is the largest number that divides both of them without leaving a remainder.For 6 and 3, the common divisors are 1 and 3. The largest of these is 3, so the HCF is 3.LCM (Least Common Multiple) of two numbers is the smallest number that is a multiple of both.To find the LCM, we typically use the formula: LCM(a, b) = (a * b) / HCF(a, b).For 6 and 3, (6 * 3) / 3 = 18 / 3 = 6. So, the LCM is 6.Note: This question helps in clearing AMCAT exam.Input format :The input consists of two space-separated integers: n1 and n2.Output format :The first line prints "HCF = " followed by an integer which is the HCF of n1 and n2.The second line prints "LCM = " followed by an integer which is the LCM of n1 and n2.Refer to the sample outputs for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n1, n2 ≤ 1000Sample test cases :Input 1 :6 3Output 1 :HCF = 3LCM = 6Input 2 :1000 1Output 2 :HCF = 1LCM = 1000
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.