12 and 13 digit product codes: Check for a valid product code (any length)

Challenge Level: Ready to expand

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user to enter a product code of any length in one line of input and adds up all the digits (every second one multiplied by 3). To add up the digits you need to start from the last digit, multiply the last digit by 1, second to last by 3 and so on. It then checks if the product code is valid (total number is a multiple of 10). Starting at the right-hand digit will make the formula work for a wide variety of product codes.

(Note that this rule will work for most of the common product codes on shop products, which use the alternating multipliers of 3 and 1. The check digit is always multiplied by 1, so starting at the right is an easy way to get the sequence correct. Common codes that you may come across are GTIN-13, GTIN-12, GTIN-8, EAN-13, EAN-8 and UPC-A, and these all use the alternating 1/3 formula. An exception is UPC-E, which is very complex. UPC-E is most common in America; it as an 8-digit code used on smaller items such as soda cans. Because the structure of UPC-E is so complex, it's best to not use them unless you have advanced students wanting a difficult challenge. UPC-E product codes can be recognised because they do not have a double line from the bars extending down between the two sets of four digits i.e. the 8 digits are all written without any gaps.)

Testing examples:

Your program should display the outputs shown in this table for the given inputs provided:

Input Output
50184385 Valid product code!
036000241457 Valid product code!
50174385 Invalid product code!
9330462119318 Valid product code!
9330463119318 Invalid product code!
036002241457 Invalid product code!

Languages

Scratch

What it should look like

Click on the green flag, enter the inputs provided in the “testing examples” to see the expected output of your program.

Recommended blocks
when green flag clicked

ask [Enter a product code:] and wait
set [total v] to ((total 1) + ((total 2) * (3)))

set [product code v] to [0]

set [total 1 v] to [0]

set [total 2 v] to [0]

set [total v] to [0]

change [product code v] by (answer)

set [index v] to (length of (product code))

set [total 1 v] to ((total 1) + (letter (index) of (product code)))

change [index v] by (-1)

set [total 2 v] to ((total 2) + (letter (index) of (product code)))

change [index v] by (-1)
repeat until <(index) < [1]>
end

if <((total) mod (10)) = [0]> then
else
end
say [This is a valid product code]

say [This is an invalid product code]
Hints
  • To add up the digits you need to start from the last digit, multiply the last digit by 1, second to last by 3 and so on. It then checks if the product code is valid (total number is a multiple of 10). Starting at the right-hand digit will make the formula work for a wide variety of product codes.

  • You can find the number digits in a number by using the length of () block unders “Operators”. For example: length of (2938339392383) //13

  • You can access a letter (or a digit) at a specified position in a string (or number) by using the letter () of [] block under “Operators”. For example: letter (4) of [18392819202910] //9

  • To access the last digit of the product code, make a variable called “index” and set it to the length of the product code. Now the last digit of the product code can be accessed by using the length of () block with the index variable and the product code.

Show Scratch solution