• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sum of the digits in an integer

 
fabio chumpitaz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which part are you having a problem with?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us some code and we will try to guide you in the right direction
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic