Chapter 6 Program.

Check Digits are useful in determining the validity of input data. Write a program that uses a modulus 11 check digit system to calculate a check digit for a 4 digit code number using the "weights" of 5, 4, 3, 2. Use the value of 00 for a check digit of 10 and "X" when the remainder is 11.

using System;

namespace CheckDigit
{

class Class1
{

[STAThread]
static void Main(string[] args)
{

int sum = 0;
int remainder, checkDigit;
string digitString

do

{
Console.WriteLine("Enter the code number:");
digitString =Console.ReadLine();
}
while (digitString.Length != 5);

char[] myChars = digitString.ToCharArray();
Console.WriteLine("The code number = {0}", digitString);

for(int i = 0; i < 4; i++)
{

if (!Char.IsDigit(myChars[i]))
{
Console.WriteLine("Code is invalid");
}

int intVal = Int32.Parse(myChars[i].ToString());
sum += intVal * (5-i);
//Console.WriteLine("The sum is {0}", sum);

    }

remainder = sum % 11;
//Console.WriteLine("The remainder is {0}", remainder);

checkDigit = 11 - remainder;
//Console.WriteLine("The Check Digit is {0}", checkDigit);

switch (checkDigit)
{

case 10:

if (myChars[4] != '0')
{

Console.WriteLine("Invalid code, check digit should be 0");
return;

}
break;

case 11:

if (myChars[4] != 'X')
{

Console.WriteLine("Invalid code, check digit should be X");
return;

}
break;

default:

if (myChars[4] != checkDigit.ToString()[0])
{

Console.WriteLine("Invalid code, check digit should be {0}", checkDigit);
return;

}
break;

}

Console.WriteLine("{0} is a valid code", digitString);

}

}

}