FastPrepReduce a Number to One Digit

Reduce a Number to One Digit

Accenture logoAccenture● EasyFULLTIMEOA
Learn

Problem statement

Given a non-negative integer num, repeatedly replace it with the sum of its decimal digits until only one digit remains.

Return that final single digit. If num is already a single digit, return it unchanged.

Function

reduceToOneDigit(num: int) → int

Examples

Example 1

num = 38return = 2

The first digit sum is 3 + 8 = 11. Summing again gives 1 + 1 = 2.

Example 2

num = 0return = 0

0 is already a single digit, so it is returned unchanged.

Example 3

num = 9998return = 8

The reductions are 9 + 9 + 9 + 8 = 35 and then 3 + 5 = 8.

Constraints

  • 0 <= num <= 10^9.

More Accenture problems

See Accenture hiring insights
public int reduceToOneDigit(int num) {
    // Write your code here.
}
num38
expected2
Checking account…