Ask Question

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.

+3
Answers (1)
  1. 14 August, 08:21
    0
    if (x > y) {

    max = x;

    }

    else {

    max = y;

    }

    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

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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. ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers