## Wednesday, March 2, 2011

### Scrub the last digit

Was working with a colleague this afternoon, we were given an algorithm to translate to code(hahah stating the obvious), our code must check if the customer's account number is valid, the code must do a checksum of the account number and verify it against the last digit.

So one part of the algorithm is, given a number how should we scrub the last digit? If a programmer is well-acclimatized with string approach, he/she will tend to use string slicing(e.g. substring, left, etc) to come up with a solution.

However, you can also use some math approach to tackle this simple problem :-)

```using System;

namespace ScrubLastDigit
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine(GetNumberStr(1763428));
Console.WriteLine(GetNumberMath(1763428));

Console.WriteLine(GetNumberStr(23423476));
Console.WriteLine(GetNumberMath(23423476));

}

// stringly-typed programming. abhored
static int GetNumberStr(int n)
{
string s = n.ToString();
return int.Parse( s.Substring(0, s.Length - 1) );
}

static int GetNumberMath(int n)
{
return n / 10;
}

}
}

```