+ 2

Kotlin Practice Digit sum question

Please check my code and tell me what is the problem. fun main(args: Array<String>) { var num = readLine()!!.toInt() var sum = 0 while (num == 0){ var i = num % 10 sum = sum + i num = num - i num = num / 10 } println(sum) } Example question. 124 Example answer. 7 Output answer. 0

22nd May 2021, 4:19 AM
철기 한
2 ответов
+ 3
replace "while (num == 0)" by "while (num != 0)" ^^
22nd May 2021, 4:23 AM
visph
visph - avatar
+ 3
Condition should be while <num> not equal to zero. Also, `num = num - i` is not necessary.
22nd May 2021, 5:22 AM
Ipang