Write a C program to find the smallest of three integers, without using any of the comparison operators.

Let 3 input numbers be x, y and z.

Method 1 (Repeated Subtraction)
Take a counter variable c and initialize it with 0. In a loop, repeatedly subtract x, y and z by 1 and increment c. The number which becomes 0 first is the smallest. After the loop terminates, c will hold the minimum of 3.

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%20%0Aint%20smallest(int%20x%2C%20int%20y%2C%20int%20z)%0A%7B%0A%20%20int%20c%20%3D%200%3B%0A%20%20while%20(%20x%20%26%26%20y%20%26%26%20z%20)%0A%20%20%7B%0A%20%20%20%20%20%20x–%3B%20%20y–%3B%20z–%3B%20c%2B%2B%3B%0A%20%20%7D%0A%20%20return%20c%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]
int main()
{
   int x = 12, y = 15, z = 5;
   printf("Minimum of 3 numbers is %d", smallest(x, y, z));
   return 0;
}
This methid doesn’t work for negative numbers. Method 2 works for negative nnumbers also.
[ad type=”banner”]

Method 2 (Use Bit Operations)
Use method 2 of this post to find minimum of two numbers (We can’t use Method 1 as Method 1 uses comparison operator). Once we have functionality to find minimum of 2 numbers, we can use this to find minimum of 3 numbers.

[pastacode lang=”c” manual=”%2F%2F%20See%20mthod%202%20of%20http%3A%2F%2Fwww.geeksforgeeks.org%2Farchives%2F2643%0A%23include%3Cstdio.h%3E%0A%23define%20CHAR_BIT%208%0A%20%0A%2F*Function%20to%20find%20minimum%20of%20x%20and%20y*%2F%0Aint%20min(int%20x%2C%20int%20y)%0A%7B%0A%20%20return%20%20y%20%2B%20((x%20-%20y)%20%26%20((x%20-%20y)%20%3E%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20(sizeof(int)%20*%20CHAR_BIT%20-%201)))%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20find%20minimum%20of%203%20numbers%20x%2C%20y%20and%20z*%2F%0Aint%20smallest(int%20x%2C%20int%20y%2C%20int%20z)%0A%7B%0A%20%20%20%20return%20min(x%2C%20min(y%2C%20z))%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20int%20x%20%3D%2012%2C%20y%20%3D%2015%2C%20z%20%3D%205%3B%0A%20%20%20printf(%22Minimum%20of%203%20numbers%20is%20%25d%22%2C%20smallest(x%2C%20y%2C%20z))%3B%0A%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/] [ad type=”banner”]

Method 3 (Use Division operator)
We can also use division operator to find minimum of two numbers. If value of (a/b) is zero, then b is greater than a, else a is greater. Thanks to gopinath and Vignesh for suggesting this method.

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20Using%20division%20operator%20to%20find%20minimum%20of%20three%20numbers%0Aint%20smallest(int%20x%2C%20int%20y%2C%20int%20z)%0A%7B%0A%20%20%20%20if%20(!(y%2Fx))%20%20%2F%2F%20Same%20as%20%22if%20(y%20%3C%20x)%22%0A%20%20%20%20%20%20%20%20return%20(!(y%2Fz))%3F%20y%20%3A%20z%3B%0A%20%20%20%20return%20(!(x%2Fz))%3F%20x%20%3A%20z%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]
intmain()
{
    intx = 78, y = 88, z = 68;
    printf("Minimum of 3 numbers is %d", smallest(x, y, z));
    return0;
}
[ad type=”banner”]