2

Here is my code:

num1= int(input('Enter the first number: '))
num2= int(input('Enter the second number: '))

def hcf(num1,num2):
    if num2==0:
        return num1
    else:
        return hcf(num2, num1%num2)
    print('The highest common factor', (hcf))

hcf(num1, num2)

It is not printing like I thought it would. I need to find the highest common factor.

martineau
  • 119,623
  • 25
  • 170
  • 301
moni55
  • 29
  • 1
  • How is that different than [gcd](https://stackoverflow.com/questions/11175131/code-for-greatest-common-divisor-in-python)? – Tomerikoo Oct 26 '20 at 18:48

2 Answers2

2

In both branch, the if and the else you use return, so no code is executed after that, the print is unreachable. You may store the result of the method (given by the return) then print it

def hcf(num1,num2):
    if num2==0:
        return num1
    else:
        return hcf(num2, num1%num2)

res = hcf(num1, num2)    
print('The highest common factor is', res)

And so you know, your highest common factor is generally known as greatest common divisor so called GCD and is present in the package math in python

from math import gcd
res = gcd(num1, num2)
print('The greatest common divisor is', res)
azro
  • 53,056
  • 7
  • 34
  • 70
0

If you mean the "greatest common divisor" by "highest common factor", you can use math.gcd:

from math import gcd

a = int(input("Number 1: "))
b = int(input("Number 2: "))
print("Greatest common divisor:", gcd(a, b))
TheClockTwister
  • 819
  • 8
  • 21