Write a Python Program to Find HCF.

Write a Python Program to Find HCF.

Highest Common Factor(HCF):
HCF, or Highest Common Factor, is the largest positive integer that divides two or more numbers without leaving a remainder.
Formula:
For two numbers a and b, the HCF can be found using the formula: HCF(𝑎,𝑏) = GCD(𝑎,𝑏)
For more than two numbers, you can find the HCF by taking the GCD of pairs of numbers at a time until you reach the last pair.
Note: GCD stands for Greatest Common Divisor.
Program
def compute_hcf(x,y):
    if(x>y):
        smaller=x
    else:
        smaller=y
    for i in range(1,smaller+1):
        if((x % i == 0) and (y % i == 0)):
            hcf=i
            
    return hcf
        
if __name__=="__main__":
    num1=int(input("Enter the number:"))
    num2=int(input("Enter the number:"))         
    print("The H.C.F. is ",compute_hcf(num1,num2))


Output:

Enter the number:54
Enter the number:24
The H.C.F. is 6



More Questions


75 . Write a Python Program to Find HCF.
76 . Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal.
77 . Write a Python Program To Find ASCII value of a character.
78 . Write a Python Program to Make a Simple Calculator with 4 basic mathematical operations.
79 . Write a Python Program to Display Fibonacci Sequence Using Recursion.
80 . Write a Python Program to Find Factorial of Number Using Recursion.
81 . Write a Python Program to calculate your Body Mass Index.
82 . Write a Python Program to calculate the natural logarithm of any number.
83 . Write a Python Program for cube sum of first n natural numbers?
84 . Write a Python Program to find sum of array.



For latest job updates join Telegram Channel: https://t.me/sateeshm