Visual C# Guide
Check Digits

The following skeleton program is to be used to calculate the check digits for EAN8 & EAN13 barcodes as well as 1SBN-10 check digits.

Th only procedures that you need to edit are the ones that calculate the different types of check digit.

static void Main(string[] args)
{
   int menItem = GetMenuChoice();
   while (menItem != 4)
   {
      string codeToConvert = GetInputString();
         switch (menItem)
         {
            case 1:
            makeEAN8(codeToConvert);
            break;
            case 2:
            makeEAN13(codeToConvert);
            break;
            case 3:
            makeISBN10(codeToConvert);
            break;
         }
      menItem = GetMenuChoice();
      }
   }

static int GetMenuChoice()
{
   //function to display menu and read in user choice
   Console.Clear();
   Console.WriteLine("Check Digit Calculation Program");
   Console.WriteLine("*******************************");
   Console.WriteLine();
   Console.WriteLine("1. EAN-8");
   Console.WriteLine("2. EAN-13");
   Console.WriteLine("3. ISBN-10");
   Console.WriteLine("4. Quit Program");
   int menChoice = System.Convert.ToInt32(Console.ReadLine());
   return menChoice;
}

static string GetInputString()
{
   Console.WriteLine();
   Console.Write("Enter the code to convert: ");
   return Console.ReadLine();
}

static void makeEAN8(string code7)
{
   //add code to calculate EAN-8 check digit and output to the console
   Console.WriteLine("Calculate EAN-8 Check Digit");
   WaitForInput();
}

static void makeEAN13(string code12)
{
   //add code to calculate EAN-13 check digit and output to the console
   Console.WriteLine("Calculate EAN-13 Check Digit");
   WaitForInput();
}

static void makeISBN10(string code9)
{
   //add code to calculate ISBN-10 check digit and output to the console
   Console.WriteLine("Calculate ISBN-10 Check Digit");
   WaitForInput();
}

static void WaitForInput()
{
   Console.WriteLine();
   Console.WriteLine("Press any key to continue");
   Console.ReadLine();
}

General Tips

The codes entered at the console are treated as strings - this avoids loss of information where codes begin with 0.

Look carefully at the EAN-8 procedure. The parameter code7 is used to store the 7 digits for which we want to calculate a check digit.

To work out the total of the 2nd, 4th and 6th items, you would write something like,

int totEven = System.Convert.ToInt32(code7.Substring(1, 1)) + System.Convert.ToInt32(code7.Substring(3, 1)) + System.Convert.ToInt32(code7.Substring(5, 1));

Remember that the first position in the substring is always 0.

Pseudocode Algorithms

EAN-8

totOdd ← 3 x (1st + 3rd + 5th + 7th)
totEven ← 2nd + 4th + 6th
bigTotal ← totOdd + totEven
IF bigTotal Mod 10 is equal to 0
   THEN checkDigit ← 0
   ELSE checkDigit ← 10 - (bigTotal Mod 10)
END IF
Output checkDigit

EAN-13 &ISBN-13

totOdd ← 1st + 3rd + 5th + 7th + 9th+ 11th
totEven ←3 x ( 2nd + 4th + 6th + 8th + 10th + 12th)
bigTotal ← totOdd + totEven
IF bigTotal Mod 10 is equal to 0
   THEN checkDigit ← 0
   ELSE checkDigit ← 10 - (bigTotal Mod 10)
END IF
Output checkDigit

ISBN-10

bigTotal ← 0
FOR i ← 0 To 8
   bigTotal ← bigTotal + (inputCode.SubString(i,1) x (i+1))
NEXT i
remainder ← bigTotal Mod 11
IF remainder is equal to 10
   THEN checkDigit = "X"
   ELSE checkDigit = ConvertToString(remainder)
END IF
Output checkDigit