Add Digits

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

Input Format

num = non-negative integer

Output Format

single-digit result after repeated digit sums

Constraints

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

Examples

Example 1:

Input:

num = 38

Output:

2

Explanation:

3 + 8 = 11, then 1 + 1 = 2.

Example 2:

Input:

num = 0

Output:

0

Explanation:

0 is already a single digit.

Loading...
Add Digits - Math DSA Problem