Write a Scala program to check which number is nearest to the value 100 among two given integers. Return 0 if the two numbers are equal.
object scala_basic { def test(x: Int, y: Int): Int = { val x_abs = Math.abs(x - 100) val y_abs = Math.abs(y - 100) if (x_abs == y_abs) 0 else if (x_abs < y_abs) x else y } def main(args: Array[String]): Unit = { println("Result: " + test(78,95)); println("Result: " + test(95,95)); println("Result: " + test(99,70)); } }
Sample Output:
Result: 95 Result: 0 Result: 99
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer