Scala function: Check if a number is even
Write a Scala function to check if a given number is even.
Sample Solution:
Scala Code:
object NumberChecker {
def isEven(number: Int): Boolean = {
number % 2 == 0
}
def main(args: Array[String]): Unit = {
val number1 = 8
val number2 = 11
println(s"Is $number1 is even? ${isEven(number1)}")
println(s"Is $number2 is even? ${isEven(number2)}")
}
}
Sample Output:
Is 8 is even? true Is 11 is even? false
Scala Code Editor :
Previous: Calculate the Power of a Number.
Next: Check if a number is a perfect square.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics