
-----------------------------------
Caceres
Wed Jul 12, 2006 10:42 pm

Addition of Digits in integer
-----------------------------------
Hello, I was just wondeirng how to add the digits of an integer.

For example:


private static int sumofbinumbers(String sin) {
two=(sin.charAt(1)-48)*2
four=(sin.charAt(3)-48)*2
six=(sin.charAt(5)-48)*2
eight=(sin.charAt(7)-48)*2
		
sum_two=(two.charAt(0)) + (two.charAt(1))
sum_four=(four.charAt(0)) + (four.charAt(1))
sum_six=(six.charAt(0)) + (six.charAt(1))
sum_eight=(eight.charAt(0)) + (eight.charAt(1))


I'm basically curious into how to add up the sums of digits of an integer.
I wa wondering if the sum_two and the part to follow will work.
Would i have to put int two = (sin.charAt(1)-48)*2.
Thanks.
P.S Sorry if this is unclear
~Caceres

-----------------------------------
wtd
Wed Jul 12, 2006 11:30 pm


-----------------------------------
There's a mathematical way of doing this.  Since I don't like just giving out easy answers...

(define (sum-digits n)
  (if (= n 0)
      0
      (+ (remainder n 10) 
         (sum-digits (floor (/ n 10))))))

-----------------------------------
Caceres
Thu Jul 13, 2006 10:13 pm


-----------------------------------
For my previous code. It keeps saying 
int cannot be dereference
Anyone know what this means?

-----------------------------------
Caceres
Thu Jul 13, 2006 10:28 pm


-----------------------------------
For my previous code. It keeps saying 
int cannot be dereference
Anyone know what this means?

-----------------------------------
[Gandalf]
Thu Jul 13, 2006 11:00 pm


-----------------------------------
It means that an int is not an object, and therefore cannot be treated as such.  An Integer, however...
