Generating Luhn Check Digit

Creating a check digit may take different forms.

Mod 10 Check Digit

This program was designed to calculate and display a check digit for an 11-digit number. The logic of this program is in accordance to the algorithm defined as 'Luhn', or Mod 10.

      ********************************************************************
      * Program Name - CHKDIG02                                          *
      *                                                                  *
      * Programmer   - Steve Croy        03/13/08                        *
      ********************************************************************
      ********************************************************************
      *               PROGRAM INTERFACE SECTION                          *
      *                                                                  *
      *  CALLED BY PROGRAMS:                                             *
      *  CALLS PROGRAMS....:                                             *
      *                                                                  *
      ********************************************************************
      ********************************************************************
      *                   Modification log                               *
      *                                                                  *
      *   Date    Programmer      Description                            *
      *                                                                  *
      ********************************************************************
     FCHKDIGDF2 CF   E             WORKSTN
      *
     D                 DS
     D  DOUBLE                 1      2  0
     D    DIG1                 1      1  0
     D    DIG2                 2      2  0
      *
     D                 DS
     D  CHK#                   1      1  0
      *
     D  numn           DS            11
     D    UPC                  1     11  0
     D                                     DIM(11)
     D  even           s              3  0
     D  odd            s              3  0
     D  total          s              3  0
     D  z              s              3  0
     D  chkn           s              1  0
     D  e              s              3  0
     D  o              s              3  0
      *

          CLEAR chkdig;
          CLEAR number;
          DOU *IN99;
             EXFMT CHKDIG01;
             IF not *IN99;
                number = %xlate(' ':'0':number);
                numn = number;
                odd = 0;
                FOR o = 11 by 2 downto 1;
                   double = upc(o) * 2;
                   odd = odd + (dig1 + dig2);
                ENDFOR;
                even = 0;
                FOR e = 2 by 2 to 10;
                   even = even + upc(e);
                ENDFOR;
                total = even + odd;
               // Calculate the check digit
               z = 10;
               DOW Total > z;
                  z = z + 10;
               ENDDO;
               CHKN = Z - TOTAL;
               CHKDIG = %EDITC(CHKN:'X');
               CHK# = CHKN;
               *IN15 = *ON;
             ENDIF;
          ENDDO;
          *INLR = *ON;
          RETURN;


1. Starting with the last digit and moving left, double the value of all the alternate digits. Values of 10 or more, add the digits together and use the resulting value for example:

8 * 2 = 16, 1 + 6 = 7.

2. Sum all the alternate (odd) digits.Sum all the even digits together. Then, add the sum of the even digits with the sum of the odd digits.

3. If the total ends in 0, the check digit is zero. Otherwise round up to the nearest multiple of 10, subtract the sum from the rounded value and the check digit is the difference of the two values.