UPC Barcode & Check Digit Calculation

UPC-A-BarcodeA barcode is an optical, representation of data that can easily be scanned by a barcode reader.

A barcode is used to store a number such as the product code of a product for sale in a shop, or the ISBN number of a book. The barcode you scan in a shop most likely uses the UPC (Universal Product Code) format which means that it consists of 12 numerical digits (0 to 9).

With a UPC barcode, the last digit is called the check digit. The check digit is used to make sure a barcode has been entered (typed) or scanned correctly to minimize human errors or scanning errors.

The check digit is a result of a complex calculation based on the 11 first digits of the barcode. Every time a barcode is scanned, the computer completes this calculation again using the first 11 digits to calculate the check digit. it then compares this with the 12th digit of the barcode. If these two are the same, the barcode scanned is most likely valid. If they are diffrent the scanner will beep to indicate that the barcode was not scanned properly.

The check digit is a fairly complex calculation that is performed in 3 steps as follows:

  1. Add the digits in the odd-numbered positions (first, third, fifth, etc.) together and multiply by three.
  2. Add the digits (up to but not including the check digit) in the even-numbered positions (second, fourth, sixth, etc.) to the result.
  3. Take the remainder of the result divided by 10 (modulo operation) and if not 0, subtract this from 10 to derive the check digit.

Check Digit Calculation

check-digit-calculation

Check Digit Validation

Your challenge consists of writing a Python program that will receive a 12-digit UPC barcode (11 digits + 1 check digit). Your Python script will then calculate the expected check digit matching the 11-digit product code. If this calculated check digit matches the 12th digit of the barcode, your program will output “Valid barcode”, if not it will output “Invalid barcode” on the screen.

Invalid Barcode?

You will then test your program with the following 6 barcodes. One of them is invalid! Use your program to find it:

# Barcode UPC-A Valid or Invalid?
#1 barcode-upc-a-1 123456789012
#2 barcode-upc-a-2 796483659834
#3 barcode-upc-a-7 572451780690
#4 barcode-not-upc-a-3 967483921543
#5 barcode-upc-a-4 485932587605
#6 barcode-upc-a-5 483025016933
#7 barcode-upca-a-6 659047763326
[pms-restrict display_to=”not_logged_in” message=” “]
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

Solution...

You are viewing this solution as part of your full membership subscription!

Python Code

Python Code

#Check Digit Validation - www.101computing.net/upc-barcode-check-digit-calculation/  
  
barcode = input("Enter 12-digit UPC-A barcode:")  
while len(barcode)!=12:  
  print("Invalid UPC-A barcode.")  
  barcode = input("Re-enter 12-digit UPC-A barcode:")  
  
checkDigit = int(barcode[11]) #12th digit  
print("Check digit: " + str(checkDigit))  
  
#Calculate check digit  
oddDigits = int(barcode[0]) + int(barcode[2]) + int(barcode[4]) + int(barcode[6]) + int(barcode[8]) + int(barcode[10])  
evenDigits = int(barcode[1]) + int(barcode[3]) + int(barcode[5]) + int(barcode[7]) + int(barcode[9])  
total = oddDigits * 3 + evenDigits  
remainder = total % 10  
if remainder == 0:  
  calculatedCheckDigit = 0  
else:  
  calculatedCheckDigit = 10 - remainder  
  
print("Calculated Check Digit: " + str(calculatedCheckDigit))    
    
#Compare both check digits to see if the barcode is valid  
if checkDigit == calculatedCheckDigit:  
  print("Valid Barcode")  
else:  
  print("Invalid Barcode")
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 24

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: