E
E
eratnikstudio2017-08-25 17:46:34
css
eratnikstudio, 2017-08-25 17:46:34

How to find the minimum and maximum of three variables without the min and max functions?

It is necessary to determine the sum of the maximum and minimum of the three numbers $a, $b, $c.
NOT APPLYING THE FUNCTIONS min and max.
With functions, I know how to do it. And without them, how to write code to determine the sum of the maximum and minimum of the three numbers $a, $b, $c.
Thank you.
Or throw where such examples are considered.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
inkShio, 2019-05-02
@toradorra

https://codepen.io/inkshio/pen/OGeqqO

A
Alexey Sklyarov, 2019-05-02
@0example

https://jsfiddle.net/u59o413n/

S
Stanislav B, 2017-08-25
@S_Borchev

$sum = $a > $b ? ($a > $c ? $a + ($b < $c ? $b : $c) : $b + $c) : ($b > $c ? $b + ($a < $c ? $a : $c) : $c + $a);

B
BoShurik, 2017-08-25
@BoShurik

$numbers = [$a, $b, $c];
sort($numbers);

echo reset($numbers) + end($numbers);

4
4iloveg, 2017-08-25
@4iloveg

function sumMinMax(...$numbers)
{
    $min = PHP_INT_MAX;
    $max = 0;
    foreach ($numbers as $key => $number) {
        if ($number > $max)
            $max = $number;
        if ($number < $min)
            $min = $number;
    }
    return $min + $max;
}

$a = 1;
$b = 2;
$c = 3;
$d = 4;
$e = 5;
$f = 6;
echo sumMinMax($a,$b,$c,$d,$e,$f);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question