Add Digits

Easy

Topics
MathSimulation

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

Example 1

Input:  n = 38
Output: 2
Explanation: 3 + 8 = 11, 1 + 1 = 2.

Example 2

Input:  n = 0
Output: 0

Constraints

  • 0 <= n <= 2^31 - 1
Run ⌘' · Submit ⌘⏎