Scala Programming: Check which number is nearest to the value 100 among two given integers
Scala Programming Basic Exercise-20 with Solution
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.
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 an Int
def test(x: Int, y: Int): Int = {
// Calculate the absolute difference between x and 100
val x_abs = Math.abs(x - 100)
// Calculate the absolute difference between y and 100
val y_abs = Math.abs(y - 100)
// Check conditions based on absolute differences
if (x_abs == y_abs) 0
else if (x_abs < y_abs) x
else y
}
// 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 78 and 95
println("Result: " + test(78,95))
// Print the result of calling test with the arguments 95 and 95
println("Result: " + test(95,95))
// Print the result of calling test with the arguments 99 and 70
println("Result: " + test(99,70))
}
}
Sample Output:
Result: 95 Result: 0 Result: 99
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): Int = {: This line defines a function named test that takes two parameters (x and y), both of type Int, and returns an Int.
- val x_abs = Math.abs(x - 100): This line calculates the absolute difference between x and 100 and assigns it to the variable x_abs.
- val y_abs = Math.abs(y - 100): This line calculates the absolute difference between y and 100 and assigns it to the variable y_abs.
- if (x_abs == y_abs) 0: This line checks if the absolute differences for x and y are equal. If true, it returns 0.
- else if (x_abs < y_abs) x: If the condition in the previous line is not true, this line checks if the absolute difference for x is less than the absolute difference for y. If true, it returns the value of x.
- else y: If none of the previous conditions are true, it returns the value of y.
- }: Closes the test function.
- 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(78,95)): This line calls the "test()" function with the arguments 78 and 95, and prints the result to the console.
- println("Result: " + test(95,95)): Another call to the "test()" function with the arguments 95 and 95.
- println("Result: " + test(99,70)): Another call to the "test()" function with the arguments 99 and 70.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check the largest number among three given integers.
Next: Check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive.
What is the difficulty level of this exercise?
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://w3resource.com/scala-exercises/basic/scala-basic-exercise-20.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics