### LANGUAGE: C++

### CHALLENGE:

Given the integer variables x , y , and z , write a fragment of code that assigns the smallest of x , y , and z to another integer variable min . Assume that all the variables have already been declared and that x , y , and z have been assigned values ).

### SOLUTION:

if(x < y){ if(x<z){ min = x; }else{ min = z; } }else{ if(y<z){ min = y; }else{ min = z; } }

also works

min = x<y?x:y;

min = min<z?min:z;

This is more strait foward.

if (x < z && x < y)

{

min = x;

}

else if (y < z)

{

min = y;

}

else

{

min = z;

}

I’m fairly certain the first posted answer is wrong. In the case where x = 2, y = 6, z = 4, the first part of the code would give us a wrong answer.

it works, i tried it.

could you do

min = x;

if(y < min){

min = y;

}

if(z < min){

min = z;

}