In: Computer Science
The last three digits of the ID is retrieved by using modulation operation. As shown in the below
last_three = (ID % 1000)
Code:
t = int(input()) #read the number of inputs
#for each input calculate the new ID
for _ in range(t):
n = int(input()) # 12 digit ID
total_sum = 0 # stores total sum of digits
last_three = (n%1000) * 10 # retrive the last digits from the ID
while(n>0):
total_sum+=n%10
n=n//10
new_id = total_sum + last_three # new Id generation
# new ID should be 4 digits
if new_id > 9999:
new_id = (n%10000)
elif new_id < 999:
new_id += 1000
#print the new ID
print(new_id)
Output:
1
978643112002
1063
Please refer to the screenshots below for correct indentations