-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCracklePop
More file actions
26 lines (22 loc) · 696 Bytes
/
CracklePop
File metadata and controls
26 lines (22 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Write a program that prints out the numbers 1 to 100 (inclusive).
//If the number is divisible by 3, print Crackle instead of the number.
//If it's divisible by 5, print Pop.
//If it's divisible by both 3 and 5, print CracklePop.
//Exploits Scala's pattern matching
def getResult(number: Int): String = (number % 3, number % 5) match {
case (0 , 0) => "CracklePop"
case (0 , _) => "Crackle"
case (_ , 0) => "Pop"
case _ => number.toString()
}
//Implemenation using recursive function
def PrintResult(x: Int): Unit = {
@annotation.tailrec
def loop(n: Int): Unit = {
println(getResult(n))
if (n >= x) Unit
else loop(n + 1)
}
loop(1)
}
PrintResult(100)