Knowee
Questions
Features
Study Tools

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.

🧐 Not the exact question you are looking for?Go ask a question

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:

  1. The function compute_hcf takes two arguments, x and y.
  2. Inside the function, a while loop runs until y becomes 0.
  3. In each iteration of the loop, x is updated to be y and y is updated to be the remainder of x divided by y.
  4. When y becomes 0, the function returns x, which is the HCF of the original x and y.
  5. The program then asks the user to input two numbers and calls compute_hcf with these numbers.
  6. Finally, it prints out the HCF.

This problem has been solved

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.

1/3

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.