Scala Programming: Check whether two given positive integers have the same last digit
Write a Scala program to check whether two given positive integers have the same last digit.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, make sure you are familiar with the following concepts:
- Modulo Operator: In Scala, the modulo operator (%) is used to find the remainder when dividing two numbers. This can help you obtain the last digit of a number.
- Mathematical Functions: Learn how to use Math.abs to find the absolute value of a number, ensuring positivity for comparison purposes.
- Boolean Logic: You will need to use a Boolean comparison to check if two values are equal.
- Functions with Parameters: Understand how to define a function that takes two parameters and returns a result (in this case, a Boolean).
Hints (Try Before Looking at the Solution!)
Consider the following hints to guide you through solving this exercise:
- Hint 1: To extract the last digit of a number, use the modulo operator with 10. This will give you the remainder, which is the last digit of the number.
- Hint 2: Use the Math.abs function to ensure that the last digits are compared as positive numbers.
- Hint 3: Check if the last digit of both numbers is the same using an equality check (==).
- Hint 4: If the last digits match, the function should return true, otherwise false.
- Hint 5: Test the function with a variety of numbers to ensure the correctness of your solution.
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?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics