Find maximum of three number in C without using conditional statement and ternary operator

前端 未结 13 2000
日久生厌
日久生厌 2020-11-29 22:25

I have to find maximum of three number provided by user but with some restrictions. Its not allowed to use any conditional statement. I tried using ternary operator like bel

13条回答
  •  情话喂你
    2020-11-29 23:18

    You can use this code to find largest out of two:

    max{a,b} = abs(a-b)/2 + (a+b)/2
    

    then use it again to find the third number:

    max{a,b,c} = max(a,max(b,c))
    

    See that this works for positive numbers you can change it to work for negative as well.

提交回复
热议问题