0

Sum of numbers kotlin

Hi, help me please i can't understand, whats wrong. fun main(args: Array<String>) { var num = readLine()!!.toInt() var res = 0 var sum = 0 while (num == 0) { res = num %10 sum += res } println (sum)

16th Apr 2021, 2:47 AM
Никита Нестреляев
Никита Нестреляев - avatar
2 Answers
+ 2
Никита Нестреляев based on what I believe you are trying to do I created this .. and as Hoh Shen Yien pointed out a similar thought 💭 while (num >= 0){ you needed to decrease the num -= 1 } https://code.sololearn.com/cJ9OM5HGaG2Z/?ref=app
16th Apr 2021, 3:52 AM
BroFar
BroFar - avatar
+ 1
It should be while (num != 0) { res = num % 10 sum += res num /= 10 because your num is supposedly decreasing towards 0, and it isn't 0 initially so the while loop will not be executed. Btw, I assumed you are finding sum of digits of a number
16th Apr 2021, 2:53 AM
Hoh Shen Yien
Hoh Shen Yien - avatar