Kotlin Exercises

1. Write a program in Kotlin to print prime numbers between 1 and 100.

Soultion

fun main(args : Array<String>)  { 
    var initial = 1
    val final = 100

    while (initial < final) {
        if (printPrimeNumber(initial))
            print(initial.toString() + " ")

        ++initial
    }
}

fun printPrimeNumber(num: Int): Boolean {
    var flag = true

    for (i in 2..num / 2) {

        if (num % i == 0) {
            flag = false
            break
        }
    }

    return flag
}
Output
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

2. Write a program to print numbers from 10 to 1 using recursion function.

Soultion

fun main(args : Array<String>)  { 
    for (x in 10 downTo 1)
    println("$x")
}
Output
10
9
8
7
6
5
4
3
2
1

3. Write a program to differentiate between val and var.

Soultion

fun main(args : Array<String>)  {  
	var x: String  = "Tiger"
    x = "Lion" //valid
    val y: String  = "Cow"
    y = "Dog" // error
    println("$x  and $y")
}
Output
Error:(5, 5) Kotlin: Val cannot be reassigned

4. Write a program in kotlin to map the following -
Item 1, Item 2, Item 3, Item 4
22, 10, 12, 33

Soultion

val map = mapOf(Pair("Item 1", 22), Pair("Item 2", 10), Pair("Item 3", 12), Pair("Item 4", 33))
println(map)
Output
{Item 1=22, Item 2=10, Item 3=12, Item 4=33}

5. Write a program in Kotlin to replace is with was respectively in given string.
He is cryling ver loud.

Soultion

fun main(args : Array<String>)  {  
	var a = "He is cryling very loud."
	val s = "${a.replace("is", "was")}"
	println(s)
}
Output
He was cryling very loud.

6. Write a program to get length of the given string.
He was cryling very loud.

Soultion

fun main(args : Array<String>)  {  
	val s = "He was cryling very loud."
	val str = "$s.length is ${s.length}"
	println(str)
}
Output
He was cryling very loud..length is 25

7. How to get average of the given array elements.
doubleArrayOf(2.5, 1.8, 3.4)

Soultion

fun main(args : Array<String>)  {  
	val x = doubleArrayOf(2.5, 1.8, 3.4)
	val y = x.average()
	println(y)
}
Output
2.5666666666666664

8. Write a program to get length of the given string.
He was cryling very loud.

Soultion

fun main(args : Array<String>)  {  
	val s = "He was cryling very loud."
	val str = "$s.length is ${s.length}"
	println(str)
}
Output
He was cryling very loud..length is 25

9. How to get average of the given array elements.
doubleArrayOf(2.5, 1.8, 3.4)

Soultion

fun main(args : Array<String>)  {  
	val x = doubleArrayOf(2.5, 1.8, 3.4)
	val y = x.average()
	println(y)
}
Output
2.5666666666666664

10. Write a program to create an array of size 10 containing.
0
2
4
6
8
10
12
14
16
18

Soultion

fun main(args : Array<String>)  {  
	val x = Array(10) { i -> i * 2 }
	for(s in x){
		println(s);
	}
}
Output
0
2
4
6
8
10
12
14
16
18

11. Write a program in kotlin to print the following statment 10 times.
n. Practice makes a man perfect.

Soultion

fun main(args : Array<String>)  {  
	repeat(10) { i ->
	println("${i + 1}. Practice makes a man perfect.")
	}
}
Output
1. Practice makes a man perfect.
2. Practice makes a man perfect.
3. Practice makes a man perfect.
4. Practice makes a man perfect.
5. Practice makes a man perfect.
6. Practice makes a man perfect.
7. Practice makes a man perfect.
8. Practice makes a man perfect.
9. Practice makes a man perfect.
10. Practice makes a man perfect.

12. Write a program in kotlin to iterate over the following map.
hashMapOf(1 to "car", 2 to "bike", 3 to "cycle", 4 to "truck")

Soultion

fun main(args : Array<String>)  {  
    var map = hashMapOf(1 to "car", 2 to "bike", 3 to "cycle", 4 to "truck")
    for ((key, value) in map) {
        println("Map[$key] = $value")
    }
}
Output
Map[1] = car
Map[2] = bike
Map[3] = cycle
Map[4] = truck





Read more articles


General Knowledge



Learn Popular Language