Lucas Crouse
Lucas Crouse

Reputation: 13

Creating an Array loop that displays 0-101 and stores the sums of their digits individually into that index

I am having trouble trying to figure this out. I need to create an application that displays the index numbers 0-101, and also stores the sum of each individual index numbers digits. for example:

  1. INDEX---------GENERATED NUMBER
  2. 0--------------------0
  3. 1--------------------2
  4. 2--------------------4
  5. 3--------------------6

And so on. Index 17 would store 25, because 17 + 1 + 7 = 25. I've got the list of index numbers down pact..

public class ArrayPractice {

public static void main(String[] args)
{
    int sum = 0;

    int[] arrayNumber = new int[102];

    for(int i = 0; i < arrayNumber.length; i++){
        arrayNumber[i] = i;
        int add = i % 10; 
        sum += add; 
        i /= 10;

        System.out.println(i);
         }
    }
} 

I also understand that to get the sum of the digits, you have to modulus 10 and then divide by 10, 17 % 10 = 7, 17 / 10 = 1 7 + 1 = 8 + 17(index) = 25.

I just keep failing to get this to work within a loop. Any help would be greatly appreciated.

Upvotes: 1

Views: 88

Answers (3)

R4N
R4N

Reputation: 2595

I think you're pretty close, but you'll most likely want to use another variable (other than i your iterator) to modify -- otherwise your iterator will continuously get messed with. You'll also want to continue doing the mod/divide operation until your initial number is 0 (i.e. you've processed all the digits). You'll also want to reset your sum for each pass through the for loop.

Here's my example code:

int[] arrayNumber = new int[102];
    for (int i = 0; i < arrayNumber.length; i++) {
        int sum = i;
        int startingNumber = i;
        while (startingNumber > 0) {
            sum += startingNumber%10;
             startingNumber /= 10;
        }
        arrayNumber[i] = sum;
        System.out.println(i + " " + sum);
    }

Upvotes: 0

Rajeev Singh
Rajeev Singh

Reputation: 3332

The problem with your code is it just sums up the last digit of the number, it won't work for numbers having digits greater than 2, you have to loop through all the digits and keep adding them to the number.

for(int i = 0; i < arrayNumber.length; i++) {    
    arrayNumber[i] = i;
    int tmp = arrayNumber[i];

    // summing up digits
    while(tmp > 0) {
        arrayNumber[i] += tmp%10; //extracting right-most digit
        tmp /= 10; // removing the right-most digit
    }
    System.out.println(i , arrayNumber[i]);
}

Upvotes: 0

Alan
Alan

Reputation: 379

Will also explain, since you have 3 digit numbers it's not enough to divide by 10 once. You have to do it until it's bigger than 0, so for 100, you get 0, 0, 1. Which is why a while loop is best, since you don't know when it will end. This works for any integer.

Just remember, for loops are meant to be used when you know when your loop has to end. While loop is used when you don't know when you will end, well you know when but not when it will happen.

EDIT: Just saw you have to add the index itself, 17 = 1 + 7 + 17. Fixed that.

    int current;
    int[] array = new int[102];
    int sum;

    for(int i = 0; i < array.length; i++){
        current = i;
        sum = 0;
        while(current != 0){
            sum += current % 10;
            current = current/10;
        }
        array[i] = sum+i;
    }

    for(int i = 0; i < array.length; i++){
        System.out.println("Index " + i + " :" + array[i]);
    }

Upvotes: 1

Related Questions