Hello, I’m working on this task:
Write a program to check which of three given integers is the nearest value to 23. Read three numbers, each from a new line, and output the result to another new line as well. For example:
>153
>-2
>66
-2
Here is my code:
namespace Exam
{
class ClosestTo23
{
static void Main()
{
var numberOne=int.Parse(Console.ReadLine());
var numberTwo=int.Parse(Console.ReadLine());
var numberThree=int.Parse(Console.ReadLine());
var numberOneCheck =closestNumberCheck(numberOne);
var numberTwoCheck =closestNumberCheck(numberTwo);
var numberThreeCheck =closestNumberCheck(numberThree);
if (numberOneCheck< numberTwoCheck && numberOneCheck<numberThreeCheck)
Console.WriteLine(numberOne);
if (numberTwoCheck<numberOneCheck &&numberTwoCheck<numberThreeCheck)
Console.WriteLine(numberTwo);
if (numberThreeCheck<numberOneCheck && numberThreeCheck<numberTwoCheck)
Console.WriteLine(numberThree);
}
static int closestNumberCheck(int number)
{
var differnceCount=0;
if (number<23)
{
for(var i=number; i<=23; i++)
{
differnceCount = differnceCount+1;
}
return differnceCount;
}
if (number>23)
{
for(var i=number; i>=23; i--)
{
differnceCount=differnceCount+1;
}
return differnceCount;
}
}
}
}
Can anyone please help me to solve it?