w3resource

Scala Programming: Check whether two given positive integers have the same last digit

Scala Programming Basic Exercise-24 with Solution

Write a Scala program to check whether two given positive integers have the same last digit.

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 the absolute value of the last digit of x is equal to the absolute value of the last digit of y
    Math.abs(x % 10) == Math.abs(y % 10)
  }
     
  // 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 123 and 456
    println("Result: " + test(123, 456))
    
    // Print the result of calling test with the arguments 12 and 512
    println("Result: " + test(12, 512))
    
    // Print the result of calling test with the arguments 7 and 87
    println("Result: " + test(7, 87))
    
    // Print the result of calling test with the arguments 12 and 45
    println("Result: " + test(12, 45))
  }
}

Sample Output:

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

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.
  • Math.abs(x % 10) == Math.abs(y % 10): This line checks if the absolute value of the last digit of x is equal to the absolute value of the last digit of y. It uses the modulo operator % to get the last digit and Math.abs to ensure a positive value.
  • }: 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(123, 456)): This line calls the "test()" function with the arguments 123 and 456, and prints the result to the console.
  • println("Result: " + test(12, 512)): Another call to the "test()" function with the arguments 12 and 512.
  • println("Result: " + test(7, 87)): Another call to the "test()" function with the arguments 7 and 87.
  • println("Result: " + test(12, 45)): Another call to the "test()" function with the arguments 12 and 45.

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

Previous: Check whether a given character presents in a string between 2 to 4 times.
Next: Convert the last 4 characters of a given string in upper case. If the length of the string has less than 4 then uppercase all the characters.

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://w3resource.com/scala-exercises/basic/scala-basic-exercise-24.php