Win a copy of Design for the Mind this week in the Design forum!

# sum of the digits in an integer

fabio chumpitaz
Greenhorn
Posts: 4
write a method to compute the sum of the digits in an integer
public static int sumDigits(long n)
sumDigits(234) returns 2+3+4=9
would someone help wiht this?

Marilyn de Queiroz
Sheriff
Posts: 9059
12
Which part are you having a problem with?

Bear Bibeault
Author and ninkuma
Marshal
Posts: 64683
86
Hi fabio, welcome to the ranch. As others have pointed out, you won't get much help if you just ask people to write your homework for you.
You will, however, gets LOTS of valuable and quality help with concepts and specific problems you might be having.
If you are having problems with getting started with this specific method, I'd venture you're having trouble separating out the individual decimal digits. If you are trying a mathematical solution, that'd be painful because decimal is not a natural numbering system for computers.
So as a hint, how could you somehow transform the incoming value in such a way that it would be easy to isolate the individual digits?
hth,
bear

James Chegwidden
Author
Ranch Hand
Posts: 201
Show us some code and we will try to guide you in the right direction