12 digit product codes: Check if total is a multiple of 10 (one line of input)

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user to enter a 12 digit product code, all in one line of input. Then adds up all the digits, every second one multiplied by 3 (1st, 3rd, and so on) and show if this total is a multiple of 10 (valid product code). For this challenge make sure the user can only enter a 12 digit product code.

Testing examples:

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

Input Output
838310022524 Valid product code!
811571013579 Valid product code!
838310032524 Invalid product code!
614141000012 Valid product code!
811571023579 Invalid product code!
664141000012 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
whenclickedaskEnter a 12 digit product code:andwait
setindexto1setproduct codetosettotal 1to0settotal 2to0settotalto0settotal 1tototal1+letterindexofproductcodechangeindexby1settotal 2tototal2+letterindexofproductcodechangeindexby1settotaltototal1*3+total2changeproduct codebyanswer
sayValid product code!sayInvalid product code!sayPlease enter a 12 digit number!
iflengthofproductcode=12thenelserepeat6iftotalmod10=0thenelse
Hints
  • You can find the number digits in a number by using the lengthof block unders “Operators”. For example: lengthof29383399238312

  • You can access a letter (or a digit) at a specified position in a string (or number) by using the letterof block under “Operators”. For example: letter4of183928192029109

  • In this challenge you need to access all the digits of the product code number entered, adding the 1st, 3rd, 5th and so on digits and storing the result in variable "total 1" and adding the 2nd, 4th, 6th and so on digits and storing the result in variable "total 2". The output displays if the sum of "total 1" multiplied by 3 and "total 2" is a multiple of 10 or not.

Show Scratch solution