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);
}
}
} |