Verify a UPC Number

0

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.

Challenge

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.

Meow Mix

Posted 2016-10-25T01:43:19.077

Reputation: 823

Question was closed 2016-10-25T01:47:31.217

This is basically the same as the Luhn error-checking algorithm, so I'm closing this as a duplicate of that challenge.

– xnor – 2016-10-25T01:47:10.207

Also, it should be noted that the formula isn't exactly right as shown. A valid sum should be divisible by ten (sum≡0(mod 10)), not equal to zero. – Geobits – 2016-10-25T02:07:16.187

No answers