w3resource

Scala Programming: Check two given integers whether either of them is in the range 100..200 inclusive

Scala Programming Basic Exercise-15 with Solution

Write a Scala program to check two given integers whether either of them is in the range 100..200 inclusive.

Sample Solution:

Scala Code:

// Define an object named scala_basic
object scala_basic {
  // Define a function named test with parameters x and y of type Int, returning a Boolean
  def test(x: Int, y: Int): Boolean = 
    {
      // Check if either (x is between 100 and 200) or (y is between 100 and 200)
      (x >= 100 && x <= 200) || (y >= 100 && y <= 200);
    }
     
   // Define the main method, which is the entry point of the program
   def main(args: Array[String]): Unit = {
      // Print the result of calling test with the arguments 100 and 199
      println("Result: " + test(100, 199))
      
      // Print the result of calling test with the arguments 250 and 300
      println("Result: " + test(250, 300))
      
      // Print the result of calling test with the arguments 105 and 190
      println("Result: " + test(105, 190))
    }
}

Sample Output:

Result: true
Result: false
Result: true

Explanation:

Here is the break down of the said Scala code:

  • object scala_basic {: This declares an object named scala_basic.
  • def test(x: Int, y: Int): Boolean =: This line defines a function named test that takes two parameters, x and y, both of type Int, and returns a Boolean. The function checks if either (x is between 100 and 200) or (y is between 100 and 200).
  • (x >= 100 && x <= 200) || (y >= 100 && y <= 200);: This is the implementation of the test function. It uses logical operators (&& and ||) to check the conditions specified above. The && represents logical AND, and || represents logical OR.
    • (x >= 100 && x <= 200): Checks if x is between 100 and 200 (inclusive).
    • ||: Logical OR, meaning either the condition on the left or the condition on the right should be true for the entire expression to be true.
    • (y >= 100 && y <= 200): Checks if y is between 100 and 200 (inclusive).
  • def main(args: Array[String]): Unit = {: This line defines the main method, which is the entry point of the program. It takes an array of strings (args) as its parameter and returns Unit (similar to void in other languages).
  • println("Result: " + test(100, 199)): This line calls the "test()" function with the arguments 100 and 199, concatenates the result with the string "Result: ", and prints the entire string to the console.
  • println("Result: " + test(250, 300)): Similar to the previous line, this calls the "test()" function with the arguments 250 and 300.
  • println("Result: " + test(105, 190)): Another call to the test function with the arguments 105 and 190.

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

Previous: Check whether one of the given temperatures is less than 0 and the other is greater than 100.
Next: Check whether three given integer values are in the range 20..50 inclusive. Return true if 1 or more of them are in the said range otherwise false.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/scala-exercises/basic/scala-basic-exercise-15.php