wood burning stoves 2.0
The moose likes Beginning Java and the fly likes sum of the digits in an integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sum of the digits in an integer" Watch "sum of the digits in an integer" New topic

sum of the digits in an integer

fabio chumpitaz

Joined: Oct 30, 2002
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

Joined: Jul 22, 2000
Posts: 9059
Which part are you having a problem with?

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

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?

[Asking smart questions] [About Bear] [Books by Bear]
James Chegwidden
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
Show us some code and we will try to guide you in the right direction

Author and Instructor, my book
I agree. Here's the link: http://aspose.com/file-tools
subject: sum of the digits in an integer
It's not a secret anymore!