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?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework