This question already has an answer here:
The UPC Code consists of a bar code and a 12 digit number to identify the product. The first eleven digits are used to tell what product it is, and the last one is for error-checking purposes.
A UPC Code is valid if the sum
3 * d1 + 1 * d2 + 3 * d3 + 1 * d4 + ... + 1 * d12 = 0
Where dn is digit n of the code.
Write a program which takes in a UPC code and does one of two things
- If the UPC Code is not 12 digits, ask for input again.
- If the UPC Code is 12 digits but invalid, ask for input again.
- If the UPC Code is 12 digits and valid, terminate.
This is code-golf, so lowest number of bytes wins.