• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sum Of Digits

 
Warren Oldham
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an exercise in which I am to "write a method to compute the sum of the digits in an integer". (Example, sumDigits(234) returns 2+3+4=9) I came up with the following code which does work, but surely there is a better way. I am very new to JAVA and not very familiar with the API. I figure there must be something in there to help with this method implementation. I may have even seen what I need and didn't recognize it. Any hints as how to proceed?
Thanks.
[CODE}
public static long sumDigits(long n)
{
int times = 0;
long sum = 0;
long number = n;
long divisor = 1;
long quotient = 0;
long digit = 0;

while ((number / divisor) > 10)
{
divisor *= 10;
times += 1;
}

while (times >= 0)
{
digit = number / divisor;
number -= (digit * divisor);
sum = sum + digit;
divisor /= 10;
times -= 1;
}

return sum;
}
[/CODE]
 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren
I don't know if this is any better than what you did, but here's the way I'd approach it.
I'd look at the String class.
Method length() returns an int which gives the number of characters of the current object.
Method charAt(int i) returns the character at index i.
So, using these two methods and some loops, plus conversion of char type back to int type, I think you could accomplish the task.
Does this help at all? It's an intersting little project...I might give it a try myself.
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren,
Following is an example, see if it meets you needs.

steve
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren,
Of if you wanted to make a separate method, and pass a long:

steve

[This message has been edited by Steve Fahlbusch (edited October 13, 2000).]
 
Warren Oldham
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to say thanks to Allen and Steve for taking the time to post a reply to my little problem.
Steve's use of the modulus operator really cleaned up the method I came up with. Since I'm trying to become better acquainted with the Java API, I'll also be trying Allen's idea of using the String class.
Thanks again guys. I really appreciate it.
Warren
 
Dipu Krishnankutty
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution seems to be good.
But what i would think better would be to take the number as a String.use the String.charAt(i)
eg:
String s=t1.getText();
int sum=0;
for(int i=0;i<s.length(;i++)>
{
sum+=Integer.parseInt(s.charAt(i));
}
t2.setString(Integer.toString(sum));
 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might as well get my solution posted, too. So many ways to accomplish the same thing!
class sod
{
static long sod(String input)
{
long total = 0;
for ( int i = 0; i < input.length(); i++ )
{
total += Character.digit(input.charAt(i),10);
}
return total;
}
public static void main( String args[])
{
if (args.length == 1)
{
System.out.println( "You entered " + args[0] );
System.out.println( "Sum of the digits = " +
sod( args[0] ) );
}
else
{
System.out.println( "Usage: sod nnn\n
where nnn is any sequence of digits" );
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic