There is my version, no use of int
arrays. While the while
instruction is a good approach in many cases, in this specific one, it is not necessary, since the bigger number will be 18 (9 * 2)
:
private Boolean ValidateIMEI(string IMEI)
{
if (IMEI.Length != 15)
return false;
var sum = 0;
var pos = 0;
foreach (var imeiChar in IMEI)
{
var imeiInt = int.Parse(imeiChar.ToString());
if (pos % 2 != 0) imeiInt *= 2;
if (imeiInt > 9) imeiInt = (imeiInt % 10) + (imeiInt / 10);
sum += imeiInt;
++pos;
}
return sum % 10 == 0;
}