Each item leaving a warehouse is stamped with a positive integer serial number. To help catch scanning errors, the warehouse computes an alternating checksum from the serial number's digits: reading the digits from left (most significant) to right, the first digit is added, the second digit is subtracted, the third digit is added, and so on, with the sign alternating for every subsequent digit.
Given a serial number, compute its alternating checksum.
A single line containing one integer n, the serial number.
Print a single integer: the alternating checksum of n's digits. The result may be negative.
Example 1
Input
886996
Expected
0
Explanation
The digits of 886996 from most to least significant are 8, 8, 6, 9, 9, 6, with alternating signs +, -, +, -, +, -. The checksum is 8 - 8 + 6 - 9 + 9 - 6 = 0.
Example 2
Input
15
Expected
-4
Explanation
The digits of 15 are 1 and 5, with signs +, -. The checksum is 1 - 5 = -4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →