Header image  
AN OBSESSION IN COMPUTING  
line decor
  HOME  ::  
line decor
   
 
Checksum
/***********************************************************************\
 * inc - 22-08-2001 - checksum.c -- http://www.incubus.co.uk
 * Windows(DOS) and Linux compatable code
 * Calculates a modulus 11 checksum, 6 or 5 digit weights
\***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*-- Check Sum Weights --*/
#define MOD_WEIGHT_6 "65432"
#define MOD_WEIGHT_5 "5432"
#define SUCCESS 1
#define ERROR 0

int checkSum (char *value);

int main (int argc, char *argv[])
{    if (argc == 2)
     {   if(!checkSum(argv[1]))
          printf("Number incorrect length (4-5 digits only)\n");
     }else
     {    printf("Please enter a number with the checkdigit missing:\n");
          printf(" 1234 = 5th digit will be the checkdigit\n");
          printf(" 12345 = 6th digit will be the checkdigit\n");
     }
     return EXIT_SUCCESS;
}

int checkSum (char *value)
{    int count;
     int temp=0;
     int x;

     count=strlen(value);
     for (x=0; x < count; x++)
          if (count == 4)
               temp+=((value[x] -48) * (MOD_WEIGHT_5[x]-48));
          else if (count == 5)
               temp+=((value[x] -48) * (MOD_WEIGHT_6[x]-48));
          else
               return ERROR;

     count = (11-(temp%11));

     printf("Number with checkdigit appended = %s",value);
     if (count == 11)
          printf("X\n");
     else if (count == 10)
          printf("0\n");
     else
          printf("%d\n",count);
     return SUCCESS;
}

 

 
       

© Copyright 1999-2008 @incubus. All Rights Reserved. All trademarks acknowledged.
incubus.co.uk || zenithpaints.co.uk || rankinstine.co.uk