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)
2 Réponses
+ 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
+ 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