KtKotlin

Kotlin exercises with solutions

Solve in editor

Write your solution right in the browser.

Open exercises with editor

1. Even Numbers Beginner

Print all even numbers from 1 to 10 – each on its own line.

Expected output: 2 ⏎ 4 ⏎ 6 ⏎ 8 ⏎ 10

Note

2..10 step 2 runs in steps of two. Or check with i % 2 == 0.

Show sample solution
fun main() {
    for (i in 2..10 step 2) {
        println(i)
    }
}

2. FizzBuzz (1–15) Beginner

Print the numbers 1 to 15. For multiples of 3 print Fizz instead, for multiples of 5 print Buzz, and for both print FizzBuzz.

Expected output: 1 ⏎ 2 ⏎ Fizz ⏎ 4 ⏎ Buzz ⏎ Fizz ⏎ 7 ⏎ 8 ⏎ Fizz ⏎ Buzz ⏎ 11 ⏎ Fizz ⏎ 13 ⏎ 14 ⏎ FizzBuzz

Note

Use when without an argument and check the "divisible by 15" case first.

Show sample solution
fun main() {
    for (i in 1..15) {
        val text = when {
            i % 15 == 0 -> "FizzBuzz"
            i % 3 == 0 -> "Fizz"
            i % 5 == 0 -> "Buzz"
            else -> i.toString()
        }
        println(text)
    }
}

3. Reverse a Word Beginner

Reverse the string "Kotlin" and print it.

Expected output: nilotK

Note

Strings have a reversed() method.

Show sample solution
fun main() {
    val wort = "Kotlin"
    println(wort.reversed())
}

4. Sum of Squares Beginner

Compute the sum of the squares of the numbers 1 to 10 and print it.

Expected output: 385

Note

A range 1..10 supports sumOf { }: (1..10).sumOf { it * it }.

Show sample solution
fun main() {
    println((1..10).sumOf { it * it })
}

5. Palindrome Function Intermediate

Write a function istPalindrom(text: String) that returns true if the text is the same forwards and backwards (ignoring upper/lower case). Print istPalindrom("Anna") and istPalindrom("Kotlin").

Expected output: true ⏎ false

Note

Convert with lowercase() and compare with reversed().

Show sample solution
fun istPalindrom(text: String): Boolean {
    val t = text.lowercase()
    return t == t.reversed()
}

fun main() {
    println(istPalindrom("Anna"))
    println(istPalindrom("Kotlin"))
}

6. Most Frequent Word Intermediate

Find the most frequent word in "a b a c b a" and print it.

Expected output: a

Note

groupingBy { it }.eachCount() returns a map of word → count; maxByOrNull { it.value } finds the largest entry.

Show sample solution
fun main() {
    val text = "a b a c b a"
    val top = text.split(" ").groupingBy { it }.eachCount().maxByOrNull { it.value }?.key
    println(top)
}

7. Primes up to 30 Intermediate

Print all prime numbers up to 30 on one line, separated by spaces.

Expected output: 2 3 5 7 11 13 17 19 23 29

Note

n is prime if (2 until n).all { n % it != 0 } holds. Join with joinToString(" ").

Show sample solution
fun main() {
    val primes = (2..30).filter { n -> (2 until n).all { n % it != 0 } }
    println(primes.joinToString(" "))
}

8. Sealed Shapes and Total Area Advanced

Define a sealed interface Form with the data classes Quadrat(seite: Int) and Rechteck(breite: Int, hoehe: Int). Write a function flaeche(f: Form): Int using a when expression (without else) and print the sum of the areas of Quadrat(3), Rechteck(2, 4), and Quadrat(1).

Expected output: 18

Note

sealed interface Form, data class Quadrat(val seite: Int) : Form. In the when: is Quadrat -> f.seite * f.seite. Sum up with listOf(...).sumOf { flaeche(it) }.

Show sample solution
sealed interface Form
data class Quadrat(val seite: Int) : Form
data class Rechteck(val breite: Int, val hoehe: Int) : Form

fun flaeche(f: Form): Int = when (f) {
    is Quadrat -> f.seite * f.seite
    is Rechteck -> f.breite * f.hoehe
}

fun main() {
    val formen = listOf(Quadrat(3), Rechteck(2, 4), Quadrat(1))
    println(formen.sumOf { flaeche(it) })
}