A product bar code used in supermarkets has the following structure:
a single product type digit (ignore this)
two groups of 5 digits
a check digit
eg 0 12345 16789 8
The check digit (8 in this case) is used to prove that the previous numbers have been properly scanned. It is calculated as follows:
Add all the digits in the odd positions:
0 + 2 + 4 + 1 + 7 + 9 = 23
Multiply this by 3:
23 * 3 = 69
Add all the digits in the even positions:
1 + 3 + 5 + 6 + 8 = 23
Add the two totals:
69 + 23 = 92
Subtract the result from the next largest multiple of 10 to give the check digit:
100 – 92 = 8
The ‘8’ is then printed after the number. How does is this used to prove the barcode’s correctness? When the product is scanned, so is the check digit. The computer calculates the check digit for itself, compares it to the scanned digit, and accepts or rejects the barcode accordingly.
You do not have access to a barcode scanner, but if you type in the barcode number, INCLUDING the check digit, your program must determine if the barcode is valid or not, and return a suitable message.
Manually work out a few barcodes that you’ve made up, then check your program with them.