Find middle number out of three numbers

Find middle number out of three numbers

There are many methods to find middle number out of three numbers. But here we mainly focus on using ‘&’ operator in first method and without using AND operator in second method. In second method we are compulsory to use if-else condition statement because only using if condition its also possible but that code is not efficient one. Here we do not use scanf to get the number because logic of finding middle number is important.

Method 1 : With ‘&’ operator

 

 Trick to form algorithm 

  • Let’s take three variable a, b and c.
  • First we check for a is middle one or not.
  • If suppose a is middle number, then possible condition could be
    b>a>c               ———-Here a remain middle
        or
    c>a>b               ———-Here a remain middle also
    (Simple put variable c in front and b at end and vice versa—funny one but true for ease of remember)
  • So we check for four different condition on first if decision making loop
    like follow

FIND MIDDLE NUMBER OUT OF THREE

 

  • Same as we check for another two variables.

 

Method 2 : Without ‘&&’ operator

 

 

 Trick to form algorithm 

  • Let’s take three variable a, b and c.
  • First we check for a > b or not.
  • If suppose a > b condition become true, then possible condition could be
    a>b>c                ———-Here a > b condition remain true
        or
    c>a>b                 ———-Here a > b condition remain true
    (Simple put variable c in front and at end—funny one but true)
  • So now according to two possibility,  we have to check one condition in each one possibility.
  • First check for b > c in first possibility. if this holds true then b is middle one.
  • If b > c become false, then check for second condition c > a in second possibility. If this holds true the a is middle one.
  • Else c is middle one.
  • Check for same if a > b is not true and execution of code goes to else part. .

Shortest Logic