int a=4999;
int b = a, c = a;
int i, j;
for (i = 0; ; i++)
if (byte.Parse(b.ToString().Substring(b.ToString().Length - 1)) == 2)
break;
else
b++;
for (j = 0; ; j++)
if (byte.Parse(c.ToString().Substring(c.ToString().Length - 1)) == 2)
break;
else
c--;
if (i >= j)
MessageBox.Show(c.ToString());
else
MessageBox.Show(b.ToString());
If you need find just nearest smaller number use this one:
int a=4999;
int c = a;
int j;
for (j = 0; ; j++)
if (byte.Parse(c.ToString().Substring(c.ToString().Length - 1)) == 2)
break;
else
c--;
MessageBox.Show( c.ToString());