13 digit product codes: Weighted sum of digits (entered one at a time)

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user to enter a 13 digit product code, entering each digit one at a time. Then adds up all the digits, every second one multiplied by 3 (2nd, 4th, and so on), and shows the total as the output.

Testing examples:

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

Input Output
9
4
2
1
9
0
2
8
1
4
7
9
2
Total: 110

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

say (join [Total: ] (total))
repeat (6)
end
ask (join (join [Enter digit ] (index)) [ of the product code:]) and wait

ask (join (join [Enter digit ] (index)) [ of the product code:]) and wait

ask (join (join [Enter digit ] (index)) [ of the product code:]) and wait
change [total 1 v] by (answer)

set [total v] to ((total 1) + ((total 2) * (3)))

change [index v] by (1)

change [total 1 v] by (answer)

change [index v] by (1)

change [total 2 v] by (answer)

set [total 1 v] to [0]

set [total 2 v] to [0]

set [index v] to [1]
Hints
  • 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 output is the sum of total 2 multiplied by 3 and total 1.

Show Scratch solution