0

I want to calculate the sum of all integers in an array which contain two digits. For example:

int[] arr1 = {1, 23, 4, 55};
int[] arr2 = {2, 5, -66, 23};

The result for the first array should be 23 + 55 = 78, while the result for the second array should be -66 + 23 = -43. How can I achieve this?

3
  • just loop the array and sum them Apr 14, 2017 at 9:08
  • Check if the value is less than or equal -10, or greater than or equal 10. Apr 14, 2017 at 9:08
  • access its array index note that array index starts with 0 A[0] A[1] A[3] also please follow naming convention by reading this oracle.com/technetwork/java/codeconventions-135099.html also note that when you say array it should be plural with s example int[] students = new int[5];
    – 0xDEADBEEF
    Apr 14, 2017 at 9:09

5 Answers 5

7

If you want to sum the elements which contain two digits only, you could use the following:

int[] arr = {1, 23, 4, 55};
int sum = 0;
for (int i : arr) {
    if (Math.abs(i) > 9 && Math.abs(i) < 100) {
        sum += i;
    }
}
0
2

To get the sum of two numbers in an array:

  1. Call the array and return a value within it by using the index of the value you want to retrieve
  2. Add that value to another call to the array (again, with desired index specified)
  3. Assign that value to the type you require

Keep in mind that when calling an array to access a stored value, use the index (or position) of the value within the array — then add one. We use this formula of indexOfValue + 1 as most languages start indexing arrays at the 0th position (although this is not always true, for example, Fortran indexes its arrays beginning with 1. So ‘integer anArray(50)’ would create an array capable of storing 50 int elements, starting at index 1 and ending at index 50). Another consequence of this structural design within programming languages is that the last value of an array can be accessed using sizeOfArray - 1.

To clarify these concepts, look at this code:

    int[] a = {1,23,4,55};
    int firstSum = a[1] + a[3]; // performs 23 + 55
    int[] b = {2,5,-66,23};
    int secondSum = b[2] + b[3]; // performs -66 + 23
    int sumOfLastValues = a[a.length - 1] + b[b.length - 1];
0

I will give you some code I have tried for it:

int sum1 = 0, sum2 = 0;
            int[] arr1 = {1, 23, 4, 55};
            for(int i: arr1){
                if(i/10 > 0 && i/100 == 0){
                    sum1 += i;
                }
                else if(i/-10 > 0 && i/-100 == 0){
                    sum1 -=i;
                }
            }
            int[] arr2 = {2, 5, -66, 23};
            for(int i: arr2){
                if(i/10 > 0 && i/100 == 0){
                    sum2 += i;
                }
                else if(i/-10 > 0 && i/-100 == 0){
                    sum2 +=i;
                }
            }
        System.out.println(sum1);
        System.out.println(sum2);

The results are:

78
-43
0

You can convert to string and check.

    for(int num : v) {
    
    // check for size
    string str = to_string(num);
    if( (str.size() == 2) || (str.size()==3 && (str[0]=='+' || str[0]=='-') ) )
        sum += num;
}

cout<<sum;
0

Runnable code can be downloaded from https://github.com/sankarees/playground/blob/main/calculate-the-sum-of-all-two-digits-integers-in-an-array

Snipper code:

public static int getSumOfTwoDigits(int[] numbers) {
    int sum = 0;
    for (int number : numbers) {
        int divident = (number < 0 ? number * -1 : number) / 10;
        if (divident > 0 && divident < 10) {
            sum += number;
        }
    }
    return sum;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.