int stepsTotal = 0; int pos = 1; int i; // Loop over array from end to start for ( i = a.length - 1; i >= 0 ; i-- ) { if (i % 2 == 1 ) { stepsTotal += a[i]; } else { stepsTotal += (a[i] * 3); } } // The closest multiple of 10 <= stepsTotal int multipleOf10 = 10 * ( stepsTotal / 10 ); // If necessary, adjust to the closest multiple of 10 >= stepsTotal if ( multipleOf10 != stepsTotal ) multipleOf10 += 10; checkdigit = multipleOf10 - stepsTotal;