You are on page 1of 1

def gcd(x,y):

if x==0:
return y
elif y==0:
return x
elif x==y:
return y
elif x>y:
return gcd(x-y,y)
else:
return gcd(x,y-x)

c=0
n = int(input("Enter the no. of elements"))
print("Enter the numbers:\n")
lst=[]
for i in range(n):
ele = int(input("Next :"))
if(ele<0 or ele==0):
print("Wrong Input! Terminated")
c=1
break
lst.append(ele)

if c!=1:
while(len(lst)>=2):
last = gcd(lst[n-2],lst[n-1])
lst = lst[0:n-2]
lst.append(last)
n=n-1

print("GCD of the given numbers is {}".format(lst[0]))

You might also like