98.7k views
4 votes
Given the integer variables x and y, write a fragment of code that assigns the larger of x and y to another integer variable max.

User Kovy Jacob
by
8.4k points

1 Answer

0 votes

Answer:

if ( x > y ) {

max = x;

}

else {

max = y;

}

Step-by-step explanation:

Assumptions:

(i) Variables x, y and max have been defined.

(ii) Since the question does not specify what to do if x and y are equal, I have assumed no such case exists. i.e x and y are never equal.

PS : The code has been written in Java.

According to the question, the larger of variables x and y is assigned to another variable, max.

To check which of x and y is greater, we could use an if...else statement like so;

if ( x > y ) { // check if x is greater than y

max = x; // if yes, assign the value of x to max

}

else { // then y is greater

max = y; // assign the value of y to max

}

User Patrick Burtchaell
by
8.4k points