Educatifu
Open menu

Sum of Digits

Easymath

Add up the individual digits of a number.

Input

A single line containing a non-negative integer n.

Output

The sum of the digits of n.

Examples

Example 1

Input
1234
Output
10

1 + 2 + 3 + 4 = 10.

Example 2

Input
0
Output
0

The only digit is 0.

Example 3

Input
99
Output
18

9 + 9 = 18.

Constraints

  • 0 <= n < 10^18 (treat it as text so it never overflows).

Hints

Reveal hint 1

You can treat the number as a string and add up its digit characters.

Reveal hint 2

Each character c can be turned into its numeric value and summed.

Reference solution (spoiler — try it yourself first)

Approach

Read the number as text and sum the numeric value of each digit character.

s = input()
print(sum(int(c) for c in s if c.isdigit()))

← All practice problems

Bring us the problem, not a perfect specification

Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.

  1. 01Share contextDescribe the workflow, constraint or risk.
  2. 02Clarify togetherWe identify missing facts and useful options.
  3. 03Choose a startAgree a focused assessment or delivery step.
Start a conversation