Find the largest sum of an individual digit of an element in array
Ex a=[23,34,56,47,88,12,]
2+3=5
3+4=7
Same way largest sum is 16
The output should be 88
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Find the largest sum of an individual digit of an element in array
Ex a=[23,34,56,47,88,12,]
2+3=5
3+4=7
Same way largest sum is 16
The output should be 88
http://mattgemmell.com/2008/12/08/what-have-you-tried/
Be sure to wrap all posted code in code tags.
If you don't understand my answer, don't ignore it, ask a question.
I would suggest creating 2 arrays, one to store those integers you've listed and one to store the combined values of the digits. To get the values of the combined digits you can use a while loop.
while(i<5) { combined[i]= (digit[i] % 10 ) + (digit[i]/10); i++; }
--- Update ---
Hey, norm I'm a little new to java as well what do you think of the solution and how would you approach this problem.
@Rango - You need to describe how the code extracts the values of the digits in a base-10 number. Just giving an OP code doesn't always help them learn and/or understand how to solve problems.
If you don't understand my answer, don't ignore it, ask a question.
Rango (April 24th, 2021)
The values you've given are/can be stored in an array. The code for the loop I provided above does as such: the loop will run as long asthe linei < 5
is what adds the digits of the numbers you originally stored in your first array. For this problem you are working with two arrays, "combined" and "digit". "Digit" holds the integers you've listed while "combined" hold the addition of the digits of the numbers. A trick to isolate the digits of a base-10 number is to use "%" and "/". Both operands are divisor, but the "/" gives the whole value of the division ex. 25 / 10 = 2.5, but java will out put only "2", whereas if you do 25%10 it will out put the remainder ie. "5". This line uses that logic to to isolate the digits. Once isolated they are combined and stored in a second array thecombined[i]= (digit[i] % 10 ) + (digit[i]/10);is a place holder in the array and is incremented by one each time the loop is excecuted.[i]
Using a hardcoded 5 in the logic makes the code inflexible. What if the array is changed to have 7 members?
It is better when working with arrays to use the array's length field: theArray.length to get the length of the array.
Another inflexible bit: What if the numbers in the array have more than 2 digits? How would you handle that?
If you don't understand my answer, don't ignore it, ask a question.