I am always on the look out for new and interesting projects to use in programming classes. Well I hope they are interesting at least. Students seem to prefer projects that are interesting, that some how relate to the real world and if they can make something else more clear so much the better. I came across an idea just the other day that I hope fits all of that.

Chris Higgins teachers math and computer science and on his blog I found his description of an exercise he ran with his math students. The standard UPC code that one finds on just about anything one can buy today has a check digit on them. The formula to calculate that check digit can also be used to verify if the code is valid or not. That is of course what check digits are all about after all. Mr. Higgins gave his algebra students the formula and asked them to validate the code on some UPC marks. I borrowed his formula below but you will want to visit his blog for the math played out with the example he shows.

The formula is as follows:

3 x (Sum of Odd Positions (i.e. 1st, 3rd, 5th,…digit)) + (Sum of Even Positions) = Z

You then take the number Z and subtract it from the next multiple of 10.

It occurred to me, perhaps because I hate doing arithmetic by hand, that this would make a couple of good programming exercises. Obviously the heart of the exercise is to add the individual digits together multiplying the sum of the odd position digits by three and then finding the difference between that result and the next multiple of 10. I can only imagine the different ways students will come up with to find the next multiple of ten. That's probably the hardest part of this problem other than potentially the input and separation of digits. It opens the door for explaining Modulo math of course but there are brut force methods and some students may very well opt for those.

Speaking of input there are several options depending on where in the course you are and how complicated you want to make things. The simplest way is to ask for each digit individually. The more complicated way is to take in the whole set of numbers as a string, break it up, convert the strings to digits and then do the math. The later allows you to work on string manipulation, conversion of strings to numbers (easier is Visual Basic than in C++ but not bad at all in C# either), and some time with parsing as a general topic.

In any case it is a great opportunity to talk about check digits, error detection and handling, and how math and computer science relate to things in the real world. Let me know if you use these ideas and how they go.