# Summing the Digits of a Number

## Challenge

• Given a non-negative integer, find the sum of its digits.

### Rules

• Your program must take a non-negative integer as input.
• Your program should output the sum of the digits of the input integer.
• Your program should be able to handle inputs up to 10^100.

### Examples

• If the input is 123, the output should be 6 (1 + 2 + 3).
• If the input is 888, the output should be 24 (8 + 8 + 8).
• If the input is 1024, the output should be 7 (1 + 0 + 2 + 4).
• If the input is 3141592653589793238462643383279502884197169399375105820974944592, the output should be 315.

### Format

• Your program should take input from standard input (stdin) and output the result to standard output (stdout).
• Your program should be runnable in a Unix-like environment.

### Test cases

Input:

123


Output:

6


Input:

888


Output:

24


Input:

1024


Output:

7


Input:

3141592653589793238462643383279502884197169399375105820974944592


Output:

315


### Scoring

• This is a code-golf challenge, so the shortest code (in bytes) wins.
• I have a feeling this is a duplicate... Commented Feb 24, 2023 at 23:04
• this is OEIS A007953
– c--
Commented Feb 24, 2023 at 23:24
• Can I take it as an array with 1 element in it? Commented Feb 25, 2023 at 0:05
• Congrats on finding a non duplicate challenge which can have an answer of 0 bytes
– Surb
Commented Feb 26, 2023 at 20:40
• @Surb 1 2 3 4 5 6 7 ... there's plenty more Commented Mar 9, 2023 at 1:33

I saw this and just had to give it a go with Java. I mean, I'm not going to win, but it's in good fun, right? Using loops, assuming that we can have multiple inputs, including 0:

class D{public static void main(String[]a){int s=0;for(String t:a)for(char c:t.toCharArray())s+=c-48;System.out.println(s);}}


That gives 125 bytes, but if we assume some assumptions, we can surely get it lower. If we assume one and only input, we don't have to loop, so we can shorten it down further. But if we also stream, we get:

class D{public static void main(String[]a){System.out.println(a[0].chars().map(i->i-48).sum());}}


Which clocks in at 97 bytes.

That's not too bad for Java.

• Welcome to Code Golf, and nice first answer! Be sure to check out Tips for golfing in Java Commented Mar 3, 2023 at 2:47
• I think for Java submissions is is common to put the boilerplate into the header and use lambdas. Here is a 31 byte port of your solution using this: Try it online! (note: I don't know any Java, just based this off of other answers I've seen on this site) Commented Mar 3, 2023 at 3:04
• Yeah, but it said it should be runnable from a command line. I just wanted to make usre it went with the rules. Your suggestion does shave off a lot of scaffolding for sure. Thanks! :) Commented Mar 6, 2023 at 11:46

=QUERY(,"select "&REGEXREPLACE(A1,"\B","+"))


# vemf, 6 bytes

ª-:48+


Input is a number literal.

try it online, uses :314159 as example

## Explanation

ª      ' represent as a codepage string
-:48  ' subtract 48 from each codepoint
' (so the codepoints are all now 0-9)
+ ' sum list


# Java 8 (OpenJDK 8), 31 bytes

s->s.chars().map(e->e-48).sum()


Try it online!

# Python, 42 Bytes

lambda x:sum(int(y) for y in list(str(x)))


Try this code online

• -7 bytes Commented May 15, 2023 at 16:49
• lambda x:sum(map(int,str(x))) Commented May 15, 2023 at 23:46

# C# (Visual C# Interactive Compiler), 18 bytes

s=>s.Sum(a=>a-48);


Try it online!

# Python 3, 33 (35!) 30 bytes

-5 thanks to c--. No strings

f=lambda n:n and n%10+f(n//10)


Try it online!

• 28 bytes
– c--
Commented May 18, 2023 at 0:01
• Since the lambda is recursive, it has to be named, so your answer is 35 bytes, or 30 bytes with my earlier suggestion.
– c--
Commented Jun 4, 2023 at 15:58

# Bash & coreutils, 21 bytes

fold -1|paste -sd+|bc


Try it online!

# Java, 27 bytes

for(s=0;n>0;s+=n%10,n/=10);


Input as Integer (n)

• Welcome to Code Golf, and nice first answer! Commented Jul 27, 2023 at 6:46
• Do note that input can't be in predefined variables - it needs to be read from stdin/argv (if using a full program) or taken from function/lambda arguments. Commented Jul 27, 2023 at 12:43

# Swift, 51 bytes

let s={String($0+0).reduce(0){Int("\($1)")!+$0}}  • You can remove the space between i: and Int. Also, you might want to use an online testing website like Try It Online so other people can verify your code is correct. Commented Jul 24, 2023 at 2:04 # Thunno 2S, 0 bytes Attempt This Online! No bytes, that's right. ## Thunno 2, 1 byte S  Attempt This Online! Alternatively, have a built-in. # Perl 5-MList::Util=sum -pF, 8 bytes $_=sum@F


Try it online!