Show problems as
Classic problem #258EasyMathSimulationNumber Theory

Add Digits

Real-world version: Single Digit Label Check
Tracked in this browser only
Read Add Digits on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Every parcel that leaves the depot carries a tracking number num, and the printer stamps a one-digit check mark next to it.

The check mark is produced like this: add up the digits of num. If that total is still two digits or longer, add up its digits as well, and keep folding the number that way until only one digit is left. That surviving digit is the check mark.

Given num, return its check mark.

Examples

Example 1

Input
num = 4917
Output
3

4 + 9 + 1 + 7 = 21, and 2 + 1 = 3. Three is a single digit, so the check mark is 3.

Example 2

Input
num = 99
Output
9

9 + 9 = 18, then 1 + 8 = 9.

Example 3

Input
num = 12345
Output
6

The digits total 15, and 1 + 5 = 6.

Example 4

Input
num = 2147483647
Output
1

The ten digits total 46, then 4 + 6 = 10, then 1 + 0 = 1. This is the largest tracking number the constraints allow.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def add_digits(num: int) -> int:
Java
public int addDigits(int num)