usingSystem;namespaceiccid{publicstaticclassLuhn{publicstaticintCalcChecksum(stringid){intcurrentDigit;intidSum=0;intcurrentProcNum=0;id=id+'0';intidLength=id.Length;if((idLength!=20)&&(idLength!=15)){thrownewSystem.ArgumentException("length not 19 or 14");}for(inti=idLength-1;i>=0;i--){stringidCurrentRightmostDigit=id.Substring(i,1);if(!int.TryParse(idCurrentRightmostDigit,outcurrentDigit)){thrownewSystem.ArgumentException("not digital");}if(currentProcNum%2!=0){if((currentDigit*=2)>9)currentDigit-=9;}currentProcNum++;idSum+=currentDigit;}currentDigit=idSum%10;if(currentDigit!=0){currentDigit=10-currentDigit;}returncurrentDigit;}}classMainClass{publicstaticvoidMain(string[]args){stringiccid="8986011101510022556";Console.WriteLine(Luhn.CalcChecksum(iccid));}}}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.