aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sum of the digits in an integer" Watch "sum of the digits in an integer" New topic
Author

sum of the digits in an integer

fabio chumpitaz
Greenhorn

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
Sheriff

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


JavaBeginnersFaq
"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
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Chegwidden
Author
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
 
Similar Threads
Please critic, rate or give some helpful advice
a problem with Palindromes please help..
Sum Of Digits
Sum the digits of an integer.
compute the sum of DIGITS in integer