Please help me with the task: Middle finder

Hello, I’m working on this task:

Write a program that reads three numbers from the console and then outputs the middle value of the three numbers. For example:
>54
>4456
>2
54

Here is my code:


namespace ConsoleInput
{
   public class TheMiddle
   {
       public static void Main(string[] args)
       {
           string aString = Console.ReadLine();
           string bString = Console.ReadLine();
           string cString = Console.ReadLine();

           int a = int.Parse(aString);
           int b = int.Parse(bString);
           int c = int.Parse(cString);

           if ((a > b) && (b > c))
           {
               Console.WriteLine(b);
           }
           else if((a > c) && (c > b))
           {
               Console.WriteLine(c);
           }
           else
           {
               Console.WriteLine(b);
           }

           

           // Perform checks here to find out which number is the middle one.
       }
   }
}


Can anyone please help me to solve it? Why is it not right?

What if a middle finder is a? In none of the scenarios above it is possible.
And what if c is bigger, than a in the first if or b is bigger than a in the second?
As an option you could split if blocks in 2 parts:

if (a < b)

and another if inside.

Thank you.

Correct
if ((a > b) && (b > c))
{
Console.WriteLine(b);
}
else if((a > c) && (c > b))
{
Console.WriteLine(c);
}
else
{
Console.WriteLine(a);
}

But it is not right too

If ((a > b) && (b >c)) How “c” can be bigger than a?

why can’t it be bigger? if (a < b ) && (c > b)c > a

Yes, sorry, really “c” bigger than “a”. But we find “middle”, so if (a<b) and (b>c) “b” is middle, need us result

if (a < b ) and (c > b) is the same as:

if ((c > b) && (b > a)) b is the middle.

1 Like

So, I can’t figure out why my program for deriving the average number is not correct

if ((c > b) && (b > a))

       {

           Console.WriteLine(b);

       }

       else if ((a > c) && (c > b))

       {

           Console.WriteLine(c);

       }

       else

       {

           Console.WriteLine(a);

       }

Because it doesn’t fit all cases.
if ((c > b) && (b > a)) is valid for b, but so if ((a > b) && (b > c)) is.

for c it’s similar

1 Like

На всяк випадок на Зустрічі 6 був розбір саме цієї задачі

Thank you very much! I understand)

1 Like