w3resource

Scala function: Check if a number is even

Scala Function Exercise-8 with Solution

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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://www.w3resource.com/scala-exercises/function/scala-function-exercise-8.php