Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Choices

Exercise 1: Even Odd

val x: Int = 10
val result = 
if x % 2 == 0 then "Even"
else "Odd"

println(result)

Exercise 2: Larger

val x: Int = 10
val y: Int = 20

val larger: Int = if x > y then x else y
println(s"The larger number is $larger.")

Exercise 3: Maximum

val x: Int = 10
val y: Int = 20
val z: Int = 15

val largest: Int = if x > y && x > z  then
  x
else if y > z then 
  y
else 
  z

println(s"The largest number is $largest.")

Exercise 4: Vowel or Consonant

val ch: Char = 'a'

val result: String = ch match 
  case 'a' | 'e' | 'i' | 'o' | 'u' => "Vowel"
  case _ => "Consonant"

println(result)

Exercise 5: Weekend


val day: String = "Saturday"

val result: String = day match 
  case "Saturday" | "Sunday" => "Weekend"
  case _ => "Weekday"

println(result)

Exercise 6: Number name

val x: Int = 4

val result: String = x match 
  case 1 => "one"
  case 2 => "two"
  case 3 => "three"
  case 4 => "four"
  case 5 => "five"
  case 6 => "six"
  case 7 => "seven"
  case 8 => "eight"
  case 9 => "nine"
  case _ => "invalid"

println(result)