Spread the love

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

#include <stdio.h>


int main()
{
        int a=10,b=11,c=12;

		//Checking for a is middle number or not
		if( b>a && a>c || c>a && a>b )
		{
			printf("a is middle number");
		}

        //Checking for b is middle number or not
		if( a>b && b>c || c>b && b>a )
		{
			printf("b is middle number");
		}

        //Checking for c is middle number or not
		if( a>c && c>b || b>c && c>a )
		{
			printf("c is middle number");
		}
		

		return 0;
}

 

 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

 

#include <stdio.h>


int main()
{
        int a=10,b=11,c=12;

		if(a>b)
		{
			if(b>c)
			{
				printf("b is middle one");
			}
			else if(c>a)
			{
				printf("a is middle one");
			}
			else
			{
				printf("c is middle one");
			}
		}
		else
		{
			if(b<c)
			{
				printf("b is middle one");
			}
			else if(c<a)
			{
				printf("a is middle one");
			}
			else
			{
				printf("c is middle one");
			}
		}

		return 0;
}

 

 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

#include<stdio.h>

int main()
{
     int a = 10,
		 b = 15,
		 c = 20;

     printf("Middle Number is %d", a>b?  ( c>a? a : (b>c? b:c) )  :  ( c>b? b : (a>c? a:c) )  );

     return 0;      	  
}