Skip to content

Instantly share code, notes, and snippets.

@sorianog
Created January 10, 2022 18:09
Show Gist options
  • Save sorianog/a6b8de49e2ca9e1850bd21ab0af4f49c to your computer and use it in GitHub Desktop.
Save sorianog/a6b8de49e2ca9e1850bd21ab0af4f49c to your computer and use it in GitHub Desktop.
/*
* Input: n, number of type Int
* Range: 1 to n (inclusive)
*
* If i is multiple of 3 & 5, print "FizzBuzz"
* If i is multiple of 3 only, print "Fizz"
* If i is multiple of 5 only, print "Buzz"
* If i is not multiple of any (above), print value of i
*
*/
fun fizzBuzz(n: Int): Unit {
for (i in 1..n) {
when {
(i % 3 == 0 && i % 5 == 0) -> println("FizzBuzz")
i % 3 == 0 -> println("Fizz")
i % 5 == 0 -> println("Buzz")
else -> println("$i")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment