I have an array {65, 62, 60, 56, 52, 50, 26, 25, 20, 06, 05, 02}.
I want to retrieve the value that is either EQUAL TO 23 OR NEAREST SMALLER than 23.
I am able to achieve nearest that is 25 but unable to get 20.
Appreciate all the help.
protected void Button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
int c = Convert.ToInt32(TextBox3.Text);
int d = Convert.ToInt32(TextBox4.Text);
int ab = 10 * a + b;
int ac = 10 * a + c;
int ad = 10 * a + d;
int ba = 10 * b + a;
int bc = 10 * b + c;
int bd = 10 * b + d;
int ca = 10 * c + a;
int cb = 10 * c + b;
int cd = 10 * c + d;
int da = 10 * d + a;
int db = 10 * d + b;
int dc = 10 * d + c;
List<int> arr = new List<int> {ab,ac,ad,ba,bc,bd,ca,cb,cd,da,db,dc};
int target = 23;
var nearestH = arr.OrderBy(x => Math.Abs(x - target)).First();
Label1.Text = nearestH.ToString();
}
Thanks