The Credit Card Project

I’m a firm believer that programming projects are best when they are about things that are interesting to students. Money tends to interest them so I have long thought about a project involving credit cards. Aren’t credit cards what a lot of students think on when thinking about buying things? I’m known that were were codes in credit card numbers for a while. I was pretty sure part of the number identified the type of card and that there was some sort of checksum involved to help do a first cut validation. I just never got around to looking things up (the Bing search) until recently Doug Peterson posted a graphic about credit card validation. There is actually more to validation then what is in the graphic but not much.


One of the key pieces of this is the checksum algorithm- the LUHN Formula. Basically some digits are doubled and added to the alternate digits which are not doubled. In most cases if the least significant digit of the result is zero the number is valid. Of course valid is not the same as “has available credit” but it’s a good first cut. Doug Peterson wrote a Small Basic routine to validate the checksum. At that link you can try it out and see the source code. Or just try it as embedded below. Doug writes about how his example and my blog post came about at Writing and Sharing Code BTW. And he is right that I should write up a solution myself and it is on my todo list.

It doesn't do a full card validation though. What else is there?

  • Different companies have different numbers of digits. American Express uses 13 and 16 digit numbers for example
  • Diners Club and American Express cards can both start with a 3 but the next digit depends on which card you are checking.

Getting it all right requires a little bit of research. I'll leave that to the reader for now. But why else is this a good project in my opinion? Lots of chances to try different decision structures. One can also verify against a user's claims of what sort of card it is or try to calculate the type of card based on the number itself. The project lends itself to a lot of solution options. Lots of options opens the door for lots of discussion about “the best” way to do things. What do you think? Good project? If you try it with students I would love to hear how it goes. Do they find it relevant and interesting? 

BTW if you are looking for projects try the projects tag to find other posts I have written about various projects.

Comments (7)
  1. Doug Peterson says:

    Thanks for sharing your thoughts about the little program, Alfred.  As you note, this is just a simple little first blush at security.  Hopefully, the Thompson millions are protected by more than a piece of software that I could write.  I look forward to seeing your code and solution for the problem.

  2. Keith Ratliff (Dracorat) says:

    Btw – the card number pictured on the card validates (ends with a 2) but when he does the math part and says =70 he ended the number with a 3. If you add all the digits according to form, you'll see it adds to 71. =)

  3. Keith Ratliff (Dracorat) says:

    I just wrote up a C# solution for checking 16 digit numbers with this algorithm:

    Critiques and questions always welcomed.

  4. Keith Ratliff (Dracorat) says:

    One final note:

    An easy number that checks out is this one:

    4444 3333 2222 1111

    Try it and see. =)

    (Makes entering tests easy anyway)

  5. Ben Chun says:

    CS Unplugged includes a similar exercise illustrating how an ISBN checksum works:…/unplugged-04-error_detection.pdf

    NCTM Illuminations also combines ISBN and credit card validation in a lesson:

  6. AlfredTh says:

    For some fun with US social security numbers take a look at…/socialsecurity.html

  7. emma says:

    hi i m appreciate for for youe thoughts.

Comments are closed.

Skip to main content