Check Digits

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

 

Problem Description

Here's a simple little program to validate credit card numbers.  

Background & Techniques

All credit card companies use a check digit to detect keying or scanning errors when credit card numbers are entered into a computer.   UPCs (Universal Product Codes) also include a check digit but calculated in a slightly different manner.  

All common credit cards use a "Modulo 10"  check digit algorithm.   In this method all even digits, counting from the right, are doubled and the digits of the doubled numbers are totaled.  To this is added the unused digits, i.e. the odd numbered positions starting from the right.    The rightmost digit is the check digit and is included in this sum.  If the result is a multiple of 10, the number is valid.  

The system catches any single data entry error and most transpositions.  Clearly an error in any of the non-doubled positions will change the sum by an amount less than 10 and result in a detected error.  Similarly an error in any of the doubled values will change the sum by an amount less than 10 and give an error.  (0 through 9 doubled values add 0,2,4,6,8,1  [2*5=10, add 1+0] ,3 [2*6=12, add 1+2],5 [etc.],7, and 9 respectively).    For transpositions, you can prove for yourself that any transposition except 0 and  9 or of two equal digits will change the sum by an amount greater than 0 but less than 10.    0 and 9 always add their own value regardless of position and thus cannot be detected if they are exchanged with another 0 or 9.   

The code provides an introduction to quite a few Delphi functions and operations:  indexing through a string as an array,  the length function for strings,  the pos function to find blanks in the entered number, the delete function to remove them,  the set inclusion test to make sure only digits are left,  the copy function to extract a substring from a string,  the ord function to get the number value of a character,  and the mod and div operators to calculate the sum.    

Addendum October 6, 2003:    A viewer wrote the other day asking if I could validate American Express card numbers.     I don't have a Amex card, so can't be sure, but the version posted today does try,  and does validate my Discover card OK, as well as "mod 11" ISBN (book) numbers and traditional "mod 10" check digit calculations.  

Addendum October 17, 2003:  Thanks to viewer Brandon Hunt, the confusion over Amex and Discover cards has been cleared up. They do use prefix digits in creating check digits.   All major cards use the same algorithm as described in the ANSI standard that governs credit cards: ANSI X4.13, "American National Standard for Financial Services - Financial Transaction Cards".   The algorithm was developed by Hans Peter Luhn, one of the early pioneers developing techniques for making computers useful.  He was granted the check digit patent in 1960, at age 64.    

 More background information can be found at  http://www.merriampark.com/anatomycc.htm  

Running/Exploring the Program 

bulletDownload source
bulletDownload  executable

Suggestions for Further Explorations

October 17- done:  Extend the testing to include American Express and Discover cards - information about their format is included as comments in the source code.  
October 6 - done: Modify the program to generate check digits for new numbers as well as validate existing ones 
Are there check digit algorithms that would be even detect even more errors than the Modulo 10 method? 

 

Created: November 16, 2002

Modified: May 15, 2018

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.