Imprimindo HCF com loop for e while em Python

Apr 17 2023
Isso ajudará a entender como o loop funciona.

#HCF with for loop
n = 17
m = 51
hcf = 1
for  i in range (1,min(n,m)+1): # Here i is increasing from 1 to min(n,m) thus we get highest number that divides both numbers.
     if m%i == 0 and n%i ==0:
            hcf = i
print(hcf)
 
#HCF with while loop
n= 17
m =51
i = 1
hcf = 1 # minimum HCF can be 1.
while min(n,m)>=i:            # Maximum limit of HCF is smallest of two number
    if n%i ==0 and m% i == 0:
         hcf = i
    i = i+1                   # Here i is increasing from 1 to min(n,m) thus we get highest number that divides both numbers.
print(hcf)

Isso ajudará a entender como o loop funciona.