Write a C program to find maximum from 3 numbers without using nested if or logical operator or conditional operator.

As mentioned in the program definition, find maximum number among 3 numbers that too without using nested-if or logical operator or conditional operator is quiet tricky. πŸ˜’

But, still we have one way to get the solution and that is, we can use bitwise operators. πŸ˜€

Bitwise Operator, as the name states, works at bit-level. Thus, to perform bit-level operations in C programming, bitwise operators are used.

OperatorsNameDescription
&Bitwise ANDCopies a bit to the result if it exists in both operands.
|Bitwise ORCopies a bit if it exists in either operand.
^Bitwise XORCopies the bit if it is set in one operand but not both.
~Bitwise ComplementFlips each bit (0->1 or 1->0).
<<Left Shift OperatorThe left operands value is moved left by the number of bits specified by the right operand.
>>Right Shift OperatorThe left operands value is moved right by the number of bits specified by the right operand.

Tutorial πŸ“Ί

Watch tutorial and learn how to solve this problem. πŸ˜‰

Steps πŸͺœ

To find maximum from 3 numbers

  1. Either we can accept 3 integers from the user and store the value in 3 variables n1, n2 and n3 or initialize these variables at the time of declaration.
  2. First, it will check if (n1>n2 & n1>n3) returns true then display a message that “n1 is maximum.” otherwise the control will move ahead.
  3. Next, if the above condition is false then it will move to else-if part and check if (n2>n1 & n2>n3) returns true or not. If it returns true then it will display the message “n2 is maximum.” otherwise the control will execute the next statement.
  4. Again, if the above condition is not true then it will check if(n3>n1 & n3>n2) returns true or not. If true then it will display “n3 is maximum.” otherwise it will execute the else part.
  5. Finally, as all above conditions return false. Thus, it will display the message, “Two or all values are identical.”

Code πŸ’»

#include <stdio.h>
#include <conio.h>

void main()
{
  int n1 = 10, n2 = 9, n3 = 11;
  clrscr();
  if (n1 > n2 & n1 > n3)
      printf("n1(%d) is maximum.", n1);
  else if (n2 > n1 & n2 > n3)
      printf("n2(%d) is maximum.", n2);
  else if (n3 > n1 & n3 > n2)
      printf("n3(%d) is maximum.", n3);
  else
      printf("Two or all values are identical.");  
  getch();
}

Example 😍

Let’s assume, the user enters 3 numbers as 10, 9 and 11 which are stored in variables n1, n2 and n3 respectively.

First of all, it will check if (n1 > n2 & n1 > n3) or not. However, 10 is greater than 9 but it is lesser than 11.

So, the first expression (10>9) will return true (1), but the second expression (10>11) will return false (0). Since, we are using bitwise AND (&) operator, so the result of 1 & 0 will be 0, which means the if condition (entire expression) is false.

Thus, it will check conditional statements and will execute the statements and display the proper message.

n3(11) is maximum.

This is how we find maximum from 3 numbers without using nested if or logical operator or conditional operator.
For more C programs, click here.

Categorized in: