Kotlin: Digit Sum | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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) }

14th Oct 2021, 8:03 PM
Mike Duris
Mike Duris - avatar
3 Answers
+ 3
Mike Duris There should be while (num != 0)
14th Oct 2021, 8:06 PM
A͢J
A͢J - avatar
+ 2
Mike Duris Sun would never be 0 but num would be so there should be num != 0
14th Oct 2021, 8:15 PM
A͢J
A͢J - avatar
0
In addition to the (sum != 0) Or opposed to?
14th Oct 2021, 8:10 PM
Mike Duris
Mike Duris - avatar