Scala Programming: Check whether a given positive number is a multiple of 3 or a multiple of 7
Scala Programming Basic Exercise-11 with Solution
Write a Scala program to check whether a given positive number is a multiple of 3 or a multiple of 7.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameter n of type Int, returning a Boolean
def test(n: Int): Boolean =
{
// Check if n is divisible by 3 or 7 using the modulo operator
n % 3 == 0 || n % 7 == 0;
}
// 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 argument 3
println("Result: " + test(3))
// Print the result of calling test with the argument 14
println("Result: " + test(14))
// Print the result of calling test with the argument 12
println("Result: " + test(12))
// Print the result of calling test with the argument 37
println("Result: " + test(37))
}
}
Sample Output:
Result: true 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(n: Int): Boolean =: This line defines a function named test that takes a parameter n of type Int and returns a Boolean. The function checks if n is divisible by 3 or 7 using the modulo operator (%). If the remainder is zero for either of these divisions, the function returns true; otherwise, it returns false.
- n % 3 0 || n % 7 0;: This line is the implementation of the test function. It checks if the remainder of the division of n by 3 is zero (n % 3 0) or if the remainder of the division of n by 7 is zero (n % 7 0). The result of this expression is a Boolean indicating whether the condition is true or false. However, there is a semicolon at the end, which makes this line a statement that does not affect the return value of the 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(3)): This line calls the "test()" function with the argument 3, concatenates the result with the string "Result: ", and prints the entire string to the console.
- println("Result: " + test(14)): Similar to the previous line, this calls the "test()" function with the argument 14.
- println("Result: " + test(12)): Another call to the "test()" function with the argument 12.
- println("Result: " + test(37)): Another call to the "test()" function with the argument 37.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Create a new string with the last char added at the front and back of a given string of length 1 or more.
Next: Create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there.
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-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics