# check digit : Java Glossary

check digit
Dating back to keypunches, id numbers have an appended check digit used to ensure that the number was keyed properly. The various types of bar code such as the UPC (Universal Product Code) you see on grocery items all have a check digit. The basic idea is if you transpose or mistype a character in entering a number the checksum won’t match the one that should be there according to the calculations. One of the most common schemes is called mod 10, used for all credit card numbers and the Canadian SIN (Social Insurance Number). Here is how to calculate a mod 10 check digit by the manual method:

e.g. to compute the expected check digit 7 for: 706-511-22?

 digit multiplier result horizonal sum = 23 7 0 6 5 1 1 2 2 ? 1 2 1 2 1 2 1 2 7 0 6 10 1 1 2 2 4
23 MOD 10 = 3
10 - 3 = 7 — the check digit

Note that the two digits of the multiplication results must be added before doing the sum.

Here is method more amenable to computer calculation:

e.g. to compute see if the check digit is as expected: 706-511-227

 digit multiplier result horizonal sum = 30 7 0 6 5 1 1 2 2 7 1 z 1 z 1 z 1 z 1 7 0 6 1 1 2 2 4 7
30 MOD 10 had better = 0. What do you know, it is!
Here is the Java code to do this where z is a function that doubles and adds digits, defined mathematically more efficiently