Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now