[Note: is this is interesting to you then this page at CS Unplugged is a must read for extra ideas and resources.]

I’ve been thinking for a while that I hadn’t played with any small projects lately. I like to play with what I would call toy projects that let me play with numbers. The other day I was entering passport data in a form and while looking at my passport I noticed that there was an extra digit in the machine readable portion of the document. Specifically while I recognized the passport number there was a tenth digit.

This was pretty obviously a check digit of some sort. Ah, I wondered, what is the formula for calculating it? Enter the Internet and a search turned up some information on how it is calculated. For me the next logical step was to create a program to make sure the formula worked with my passport number. Oh sure I could do it by hand but where is the fun in that.

And besides a program lets me think about basic principles. A formula of course. Data parsing naturally. Probably an array might be involved. It is in my solution it is but it might not be in other solutions. So what is the formula involved?

Each individual digit is multiplied by a weight. The first digit is multiplied by 7; the second by 3; the third by 1 and then the cycle repeats. These products are added together with the result divided by 10. The remainder is the check digit.

So if the number is 123456789 (Passport numbers are all 9 digits long) you have:

1*7 + 2*3 + 3*1 + 4*7 + 5*3 + 6*1 + 7*7 + 8*3 + 9*1 = 147

Dividing by 10 leaves a remainder of 7 which is the check digit. Easy enough.

Now things get complicated if the passport “number” uses letters but US passports don’t. If you want to include that in the problem the letters (upper case only) convert to numbers with “A” converting to 10 incrementing up to “Z” at 35. The rest of the formula stays the same. But adding that complication makes the whole project more “interesting.”

One of the things I would think about with this project with students is data validation. Things like:

- Are there the right number of digits? Note that the same formula is used for other numbers on the passport, like date of birth, that are not nine digits long.
- Are the characters in the data valid? Digits or uppercase letters between A and Z.
- What are good error messages? Is it enough to say wrong length or should you say how many are required? Of should you say too long or too short?

Of course to me the whole concept of check digits as tools for validation of data is well worth some discussion in class on its own merit. It might be useful for students to look up other numbers that use check digits and discuss why there are there.

I threw my solution together in about 11 lines of code (in VB .NET) and about half of those lines are data declarations. I’ll leave doing it in other languages or in fewer lines of code as an exercise for the student though.

PingBack from http://geeklectures.info/2007/12/20/a-simple-check-digit-project/

Cool, I didn’t know passports had this feature. Along the same lines, you could also have students implement a credit card number validity check:

http://www.webreference.com/programming/carts/chap7/3/

I made a similar project involving the UPC code of products. It seemed really easy to implement. The students did not have any trouble following the formula.

Link: http://www.mrhiggins.net/blog/2006/09/02/confidence-is-flowing/