Ask Question
19 July, 18:58

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.

+4
Answers (1)
  1. 19 July, 20:50
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers