0
Kotlin: Digit Sum
Having trouble with one of the Kotlin lessons, it is having me write a code to find the sum of numbers. For example, find the sum of 123 for 6, however my code keeps returning the output of 0. fun main(args: Array<String>) { var num = readLine()!!.toInt() var sum = 0 while (sum != 0) { var i = num % 10 num = num / 10 sum = sum + i } println(sum) }
3 Respostas
+ 3
Mike Duris
There should be
while (num != 0)
+ 2
Mike Duris
Sun would never be 0 but num would be so there should be num != 0
0
In addition to the
(sum != 0)
Or opposed to?