[Math] How to find the greatest number among 3 integer numbers

algebra-precalculus

Can anyone explain to me where & how can I use this equation. I was told to make a program that reads 3 integer numbers and prints the greatest one using the following formula. But how?

$$\operatorname{Major} AB = \frac{a+b+abs(a-b)}{2}$$

Best Answer

The equation you've given returns the maximum of two numbers; that is,

$$\max\{a, b\} = \frac{a + b + |a - b|}{2}$$

To see this, simply note that if $a \ge b$, we have

$$a + b + |a - b| = a + b + (a - b) = 2a$$

and if $a < b$,

$$a + b + |a - b| = a + b - (a - b) = 2b$$ Then to find the largest of three numbers, simply compare the first two, and then compare the result with the third number.

Related Question