Write a program to find out the Highest Common Factor for given two numbers.
Question
Write a program to find out the Highest Common Factor for given two numbers.
Solution
Sure, here is a simple program to find the Highest Common Factor (HCF) of two numbers. This program is written in Python.
# Function to find HCF
def compute_hcf(x, y):
while(y):
x, y = y, x % y
return x
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
hcf = compute_hcf(num1, num2)
print("The HCF is", hcf)
Here's how it works:
- The function
compute_hcftakes two arguments,xandy. - Inside the function, a while loop runs until
ybecomes 0. - In each iteration of the loop,
xis updated to beyandyis updated to be the remainder ofxdivided byy. - When
ybecomes 0, the function returnsx, which is the HCF of the originalxandy. - The program then asks the user to input two numbers and calls
compute_hcfwith these numbers. - Finally, it prints out the HCF.
Similar Questions
HCFWrite a program to find out the Highest Common Factor for given two numbers.
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
Problem StatementAnna is working on a program to find common factors of two unsigned integers. Design a program that:Takes two unsigned integers: n1 and n2, as input.Identifies and prints all common factors of these numbers.For example, Let us take two numbers 24 and 36. The factors of 24 are 1, 2, 3, 4, 6, 8, 12, 24. The factors of 36 are 1, 2, 3, 4, 6, 9, 12, 36. The common factors of 24 & 36 are 1,2,3,4,6,12 Ensure the program accurately identifies common factors, providing correct results for various inputs.Input format :The input consists of two unsigned integers n1 and n2 separated by a space.Output format :The output prints the common factors of n1 and n2, separated by a space.
Greatest Common Factor
Find the highest common factor of $$210 and $$400.
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.