w3resource

Scala Programming: Check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive

Scala Programming Basic Exercise-21 with Solution

Write a Scala program to check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive.

Sample Solution:

Scala Code:

object scala_basic {
  def test(x: Int, y: Int): Boolean = {
     List(x, y).forall { m => m >= 40 && m <= 50 } || List(x, y).forall { n => n >= 50 && n <= 60 }
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test(78,95));
      println("Result: " + test(25,35));
      println("Result: " + test(40,50));      
      println("Result: " + test(55,60));      
    }
  }

Sample Output:

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

Scala Code Editor :

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

Previous: 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.
Next: Write a Scala program to find the larger value from two positive integer values in the range 20..30 inclusive, or return 0 if neither is in that range.

What is the difficulty level of this exercise?