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.

Results 1 to 6 of 6

Thread: Im beginner need a solution for one logic

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Rango (April 24th, 2021)

  6. #5
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. SOLUTION
    By tefo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 16th, 2014, 09:41 PM
  2. HR Solution
    By sharif_jp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 28th, 2014, 07:27 AM
  3. exercise solution ???
    By somebodyonearth in forum Java Theory & Questions
    Replies: 1
    Last Post: March 11th, 2012, 12:52 PM
  4. Solution
    By Suresh.athi in forum The Cafe
    Replies: 1
    Last Post: January 19th, 2012, 08:46 AM
  5. Please help me with the solution
    By abhyudayaupadhyay in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 6th, 2011, 03:21 PM