0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Find Sum of Digits of a Number

You can find the sum of digits in Kotlin by converting the number to a string and summing each digit using number.toString().sumOf { it.digitToInt() } or by repeatedly extracting digits with number % 10 and adding them.
📋

Examples

Input123
Output6
Input0
Output0
Input99999
Output45
🧠

How to Think About It

To find the sum of digits, think of the number as a collection of single digits. You can either treat it as a string and add each character's numeric value or repeatedly take the last digit using % 10 and add it to a total, then remove that digit by dividing by 10 until the number is zero.
📐

Algorithm

1
Get the input number.
2
Initialize a sum variable to zero.
3
While the number is greater than zero:
4
Extract the last digit using modulo 10.
5
Add the digit to the sum.
6
Remove the last digit by dividing the number by 10.
7
Return or print the sum.
💻

Code

kotlin
fun main() {
    var number = 123
    var sum = 0
    while (number > 0) {
        sum += number % 10
        number /= 10
    }
    println(sum)
}
Output
6
🔍

Dry Run

Let's trace the number 123 through the code to find the sum of its digits.

1

Initial values

number = 123, sum = 0

2

First iteration

sum = 0 + 123 % 10 = 3, number = 123 / 10 = 12

3

Second iteration

sum = 3 + 12 % 10 = 3 + 2 = 5, number = 12 / 10 = 1

4

Third iteration

sum = 5 + 1 % 10 = 5 + 1 = 6, number = 1 / 10 = 0

5

Loop ends

number is 0, stop loop, sum = 6

numbernumber % 10sumnumber / 10
1233312
12251
1160
💡

Why This Works

Step 1: Extract last digit

Using number % 10 gets the last digit of the number because modulo gives the remainder after division by 10.

Step 2: Add digit to sum

Add the extracted digit to the running total stored in sum.

Step 3: Remove last digit

Divide the number by 10 using integer division to remove the last digit and prepare for the next iteration.

Step 4: Repeat until done

Continue until the number becomes zero, meaning all digits have been processed.

🔄

Alternative Approaches

String conversion and sumOf
kotlin
fun main() {
    val number = 123
    val sum = number.toString().sumOf { it.digitToInt() }
    println(sum)
}
This method is concise and easy to read but involves converting the number to a string, which may be less efficient for very large numbers.
Recursive approach
kotlin
fun sumDigits(n: Int): Int = if (n == 0) 0 else n % 10 + sumDigits(n / 10)

fun main() {
    println(sumDigits(123))
}
This uses recursion to sum digits, which is elegant but can cause stack overflow for very large numbers.

Complexity: O(d) time, O(1) space

Time Complexity

The time depends on the number of digits d in the number because each digit is processed once in the loop.

Space Complexity

Only a few variables are used, so space is constant O(1).

Which Approach is Fastest?

The loop with modulo and division is fastest and uses less memory than string conversion or recursion.

ApproachTimeSpaceBest For
Modulo and division loopO(d)O(1)Efficient for all number sizes
String conversion and sumOfO(d)O(d)Simple code, good for small to medium numbers
Recursive approachO(d)O(d)Elegant but risky for large numbers due to stack use
💡
Use number % 10 to get the last digit and number / 10 to remove it when summing digits.
⚠️
Forgetting to update the number by dividing by 10 inside the loop causes an infinite loop.