# Sum Of Digits

Warren Oldham

Greenhorn

Posts: 2

posted 16 years ago

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]

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

posted 16 years ago

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.

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.

Allen

Warren Oldham

Greenhorn

Posts: 2

posted 16 years ago

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

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

Allen Alchian

Ranch Hand

Posts: 83

posted 16 years ago

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" );

}

}

}

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" );

}

}

}

Allen