Find the difference numbers 7 5 1
Output: 7-5=2, 5-1=4, 7-1=6.
So output is least difference 2
diff = []
print("Enter list (separated by a space):")
a = input().split()
for i in range(len(a)):
a[i] = int(a[i])
for i in range(len(a)-1):
for j in range(i+1, len(a)):
diff.append(a[i] - a[j])
print(f"{a[i]}-{a[j]}={a[i]-a[j]}", end=" ")
min = diff[0]
for i in range(len(diff)):
if min > diff[i]:
min = diff[i]
print(f"\nLeast difference: {min}")
Comments
Leave a comment