# 13 digit product codes: Calculate the last digit (one line of input)

Learning outcomes

Students will be able to:

## Requirement:

Write a program that asks the user to enter the first 12 digits of a product code, in one line of input, and works out the last digit as the output. For this challenge make sure the user can only enter a 13 digit product code (the first 12 digits).

## Testing examples:

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

Input Output
941887000085 The last digit is: 2
941527700012 The last digit is: 6
930072885454 The last digit is: 3
932835000371 The last digit is: 9

## 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 first 12 digits of the barcode:] and wait
repeat (6)
end

if <(length of (first 12 digits)) = [12]> then
else
end
say [You must enter a 12 digit number!]

say (join [The last digit of the product code is: ] (last digit))
set [index v] to [1]

set [first 12 digits v] to [0]

set [total 1 v] to [0]

set [total 2 v] to [0]

set [last digit v] to [0]

set [last digit v] to (((0) - ((total 1) + ((total 2) * (3)))) mod (10))

set [first 12 digits v] to (answer)

set [total 1 v] to ((total 1) + (letter (index) of (first 12 digits)))

change [index v] by (1)

set [total 2 v] to ((total 2) + (letter (index) of (first 12 digits)))

change [index v] by (1)
Hints
• For the 12 digits entered, you need to add up 1st, 3rd, 5th and so on digits and store the result in a variable called "total 1". Then add up 2nd, 4th, 6th and so on digits and store the result in variable called "total 2". The last digit (check digit) is then calculated by subtracting the sum of "total 2" multiplied by 3 and "total 1" from 0 mod 10.

• 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