w3resource

Scala Programming: Check two given integers, and return true if one of them is 30 or if their sum is 30

Scala Programming Basic Exercise-4 with Solution

Write a Scala program to check two given integers, and return true if one of them is 30 or if their sum is 30.

Sample Solution:

Scala Code:

object scala_basic {
  def test(x:Int, y:Int) : Boolean  =
    {
     x == 30 || y == 30 || x + y == 30
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test(30, 0));
      println("Result: " + test(25, 5));
      println("Result: " + test(30, 20));
      println("Result: " + test(25, 20));
   }
}

Sample Output:

Result: true
Result: true
Result: true
Result: false

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Scala program to get the absolute difference between n and 51. If n is greater than 51 return triple the absolute difference.
Next: Write a Scala program to check a given integer and return true if it is within 20 of 100 or 300.

What is the difficulty level of this exercise?