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:

object scala_basic {
  def test(x: Int, y: Int): Boolean = {
      Math.abs(x % 10) == Math.abs(y % 10);
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test(123, 456));
      println("Result: " + test(12, 512));
      println("Result: " + test(7, 87));
      println("Result: " + test(12, 45));
    }
  }

Sample Output:

Result: false
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 check whether a given character presents in a string between 2 to 4 times.
Next: Write a Scala program to 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?