Results 1 to 2 of 2
Thread: Array
 03072010, 01:45 PM #1Member
 Join Date
 Mar 2010
 Posts
 1
 Rep Power
 0
 03072010, 02:05 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
This can easily be solved using a little recursive method; a few simple observations:
1) let S(n) be the function we're looking for.
2) the sum of the digits in the number zero (0) equals 0, i.e. S(0) == 0
2) let a number be written as the digits d_1 d_2 ... d_n then the sum of all the digits equals S(d_1 d_2 ... d_n1)+d_n
Observations 2) and 3) lead to the following function:
Java Code:int S(int n) { if (n == 0) return 0; // observation 2) return S(n/10)+n%10; // observation 3) }
kind regards,
JosLast edited by JosAH; 03072010 at 02:15 PM.
Similar Threads

convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09132011, 11:32 AM 
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10122009, 11:41 AM 
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09102009, 09:12 AM 
How to transfer 1D array in JAVA to 3D array in C
By fishwater00 in forum New To JavaReplies: 0Last Post: 07312009, 06:24 PM 
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03312009, 06:40 AM
Bookmarks