I'm trying to get a checksum of a UPC code using the following algorithm:
- working from right to left, sum the digits in the odd-numbered positions (first, third, fifth, etc.)
- multiply the total by three
- sum the digits in the even-numbered positions (second, fourth, sixth, etc.)
- add the results of (1) and (2) together, and
- subtract the total from the closest multiple of ten greater than or equal to that total
The result should be the check digit.
Here's my code:
int[] a = {1, 2, 3, 4, 5}
checksum = 0;
int i = 0;
while ( a[i] < a.length-1 )
{
checksum += a[i] + a[i - 1] ;
i++;
}
checksum = ;
Any ideas?