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

# Thread: Im beginner need a solution for one logic

1. ## Im beginner need a solution for one logic

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

2. ## Re: Im beginner need a solution for one logic

http://mattgemmell.com/2008/12/08/what-have-you-tried/

Be sure to wrap all posted code in code tags.

3. ## Re: Im beginner need a solution for one logic

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.

4. ## Re: Im beginner need a solution for one logic

@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.

5. ## The Following User Says Thank You to Norm For This Useful Post:

Rango (April 24th, 2021)

6. ## Re: Im beginner need a solution for one logic

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 as
` i < 5`
the line
` combined[i]= (digit[i] % 10 ) + (digit[i]/10);`
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 the
` [i]`
is a place holder in the array and is incremented by one each time the loop is excecuted.

7. ## Re: Im beginner need a solution for one logic

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?