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));
Console.ReadLine();
}
// 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;
}
}
}
No comments:
Post a Comment