# 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 ).

5

### 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;
}
}

```

• Steve Bobson says:

also works

min = x<y?x:y;
min = min<z?min:z;

• Mark Goyette says:

This is more strait foward.

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

• Alexandre Guay says:

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.

• Phil says:

it works, i tried it.

• Mike says:

could you do

min = x;

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