| (8) UPC Check Digit Scanner

An example of a Universal Product Code (UPC)
The last digit of the UPC code is called a check digit. This digit lets the scanner determine if it scanned the number correctly or not. Here is how the check digit is calculated for the other 11 digits, using the code 811138000202 from the example shown above:
811138000202 (odd position digits in BLACK, even in RED and check digit in BLUE)
1. Add together the value of all of the digits in odd positions (digits 1, 3, 5, 7, 9 and 11).
8 + 1 + 3 + 0 + 0 + 0 = 12
2. Multiply that number by 3.
12 * 3 = 36
3. Add together the value of all of the digits in even positions (digits 2, 4, 6, 8 and 10).
1 + 1 + 8 + 0 + 2 = 12
4. Add this sum to the value in step 2.
36 + 12 = 48
5. Take the number in Step 4. To create the check digit, determine the number that, when added to the number in step 4, is a multiple of 10.
48 + 2 = 50
The check digit is therefore 2.
Each time the scanner scans an item, it performs this calculation. If the check digit it calculates is different from the check digit it reads, the scanner knows that something went wrong and the item needs to be rescanned.
Write a program that read UPC’s from the file upcfile.txt in the txtfiles folder. It should read, check for correctness and display all UPC’s that fail the check digit test described above.
These are the ones that should fail (and thus be output to the screen).
993728472798
516069254923
339977717632
418114501927
951733717280
|