UPC Check Digit



UPC Check Digit

A check digit is a type of a simple error detection scheme, meant to catch incorrectly entered data, such as typos. Credit cards, for example, use Luhn algorithm to generate account numbers. Alternatively a check digit could be a character appended to the end of data that is being validated.

[pic]

The final digit of a Universal Product Code is a check digit computed as follows:

1. Add the digits (up to but not including the check digit) in the odd-numbered positions (first, third, fifth, etc.) together and multiply by three.

2. Add the digits (up to but not including the check digit) in the even-numbered positions (second, fourth, sixth, etc.) to the result.

3. Take the remainder of the result divided by 10 (modulo operation) and subtract this from 10 to derive the check character.

For instance, the UPC-A barcode for a box of tissues is "03600024145F". The last character is "F", and if the other numbers are correct then the check digit calculation must produce 6 to indicate that F is the 6th letter in the alphabet.(A-1, B-2, C-3 and so on)

1. We add the odd number digits: 0+6+0+2+1+5 = 14

2. Multiply the result by 3: 14 × 3 = 42

3. We add the even number digits: 3+0+0+4+4 = 11

4. We add the two results together: 42 + 11 = 53

5. To calculate the check digit, take the remainder of (53 / 10), which is also known as (53 % 10), and subtract from 10. Therefore, the check digit value is 7.

The barcode is not valid.

Another example: take the UPC 07061700609B. The sum of odd-numbered digits is 0+0+1+0+6+9 =16, and the sum of the even numbered digits is 7+6+7+0+0= 20. The total sum is 3×16 + 20 = 68. 68 % 10 is 8. 10 – 8 is 2. So the code is valid, because B is the second letter in the alphabet.

The user will enter a 12 character ( 11 digits and 1 character) UPC code that has an invalid check character. You should produce the UPC code with the correct check character.

Sample Input #1

07061700609C

Sample Output #1

07061700609B

Sample Input #2

03600029145X

Sample Output #2

03600029145B

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download