# what is the logic behind the 'UPC -A' check digit?

In UPC-A barcode symbology, the 12th digit is known as check digit and it is used by the scanner to determine if it scanned the number correctly or not.

    the check digit is calculated as follows:
1.Add the digits in the odd-numbered positions (first, third, fifth, etc.) together and multiply by three.
2.Add the digits in the even-numbered positions (second, fourth, sixth, etc.) to the result.
3.Find the result modulo 10 (i.e. the remainder when divided by 10.. 10 goes into 58 5 times with 8 leftover).
4.If the result is not zero, subtract the result from ten.

For example, in a UPC-A barcode "03600029145x" where x is the unknown check digit, x can be calculated by
adding the odd-numbered digits (0 + 6 + 0 + 2 + 1 + 5 = 14),
multiplying by three (14 × 3 = 42),
adding the even-numbered digits (42 + (3 + 0 + 0 + 9 + 4) = 58),
calculating modulo ten (58 mod 10 = 8),
subtracting from ten (10 − 8 = 2).
The check digit is thus 2.


What is the logic behind calculating 'check digit' in such manner?
Can't two different combination of digits produce the same check digit?

-
It provides a quick check for errors. It doesn't catch all errors, but will catch many. Like checking that your car has an even number of wheels :-). – copper.hat Feb 17 '13 at 8:27

Can't two different combination of digits produce the same check digit? --

Yes. But that's not really the point. If the given check digit doesn't agree with the value computed from the other digits, then there's an inconsistency, so something is wrong. Either the check digit is wrong, or some of the other digits are wrong.

If the check digit is correct, this doesn't guarantee that the other digits are correct. There are various check-digit algorithms. They are designed so that simple common errors (like one digit wrong, or a transposition of two digits) will cause a change in the calculated check digit. So, if the check digit is correct, at least you know that no simple errors of this sort have occurred. Different algorithms target different types of errors.

-
Just to clarify, when you say transposition of two digits, you mean of course two consecutive digits (or, more generally, an odd-numbered and an even-numbered one), which is after all a common error. The exchange of the first and third digit, say, is not caught by the code. – Andreas Caranti Feb 17 '13 at 9:35
@Andreas -- yes, that's right. Actually, I thought "transposition" always means swapping of adjacent digits. But, anyway, it's better to be explicit. – bubba Feb 17 '13 at 10:08
I know what you meant, but a source people are likely to look up en.wikipedia.org/wiki/… says otherwise, so, yes, better to be explicit. – Andreas Caranti Feb 17 '13 at 10:11

Just a remark on your question

Can't two different combination of digits produce the same check digit?

Of course this happens, after all there are $10^{11}$ possibilities for the first $11$ digits, and only $10$ for the $12$-th one. So plenty of valid codes will share the same $12$-th digit, and a mistake that takes you from one to the other can't be caught. But as noted by @bubba, the code aims at catching two of the commonest errors.

-