Problem · Math
EasyArcesium logoArcesiumINTERNOA

Problem statement

Given a non-negative integer num, repeatedly add all of its decimal digits until the result has only one digit.

Return that single-digit result.

Function

addDigits(num: int) → int

Examples

Example 1

num = 38return = 2

The process is 38 -> 3 + 8 = 11 -> 1 + 1 = 2.

Example 2

num = 0return = 0

The input already has one digit, so the result is 0.

Example 3

num = 99999return = 9

The digit sum is 45, and 4 + 5 = 9.

Constraints

  • 0 <= num <= 2^31 - 1

More Arcesium problems

drafts saved locally
public int addDigits(int num) {
  // Write your code here.
}
num38
expected2
checking account