Funcia
Tutorialmath

Sum of Digits

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Given a non-negative integer n, return the sum of its decimal digits. For n = 1234 that is 1 + 2 + 3 + 4 = 10.

Examples

nresult
123410
00
90918

Example 1 — 1 + 2 + 3 + 4 = 10.

Example 2 — The digit sum of 0 is 0.

Example 3 — 9 + 0 + 9 = 18.

This is a tutorial problem: there are no performance tests, just return the correct value.

Function signature

solution(n: int) → int

Constraints

  • 0≤0 \le n ≤109\le 10^9

Expected complexity

Time
O(log N)
Space
O(1)

Examples

nExpected
123410
00
90918

Hint

Hint

n % 10 is the last digit and n / 10 (n // 10 in Python) drops it. Repeat "add the last digit, divide by 10" until n becomes 0.

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1200 msestimated
C# 14 (.NET 10)1200 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness10012verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.