Predict the output of the Python code given below:
def Diff(N1,N2):
if N1>N2:
return N1-N2
else:
return N2-N1
NUM= [10,23,14,54,32]
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),'#', end=' ')
Answer:
Output: 22 # 40 # 9 # 13 #
Explanation:
- The function Diff( ) takes 2 numbers as parameters and finds the difference by subtracting the smaller number from the larger number and returns the difference.
- NUM is an array of 5 numbers.
- The for loop iterates from 4 to 0 by decrementing by 1.
- A and B are assigned 2 consecutive numbers from the array NUM and the difference is calculated and printed. This is repeated for all 2 consecutive numbers in the array.