Code a Math Quiz or Calculator
Lesson preview: the Mini Lesson and Vocabulary are open to everyone. Guided practice, exercises, the parent checklist and the printable PDF worksheet come with full access (free 7-day trial or membership).
Learning Objectives
- 1Read pseudocode that uses variables, math expressions, and conditionals
- 2Trace step by step what a short program prints for given inputs
- 3Translate a math idea into pseudocode using IF, ELSE, and PRINT
- 4Plan a simple math quiz or calculator program with inputs and outputs
Mini Lesson
A computer program is a list of steps a machine follows in order. When you write code that does math, you turn an idea like average three quiz scores into a clear set of instructions. In this lesson you will learn how to plan a small program — a math quiz or a basic calculator — using pseudocode. Pseudocode is plain-English code: it does not run on a computer, but it shows the logic you would type into a real programming language.
The big four ideas
- Variable: a named box that holds a value. Example: score = 85. The name is on the left, the value is on the right.
- Expression: a formula that computes a value. Example: average = (a + b + c) / 3. The right side is calculated, then stored in the left side.
- Conditional: an IF / ELSE choice. Example: IF score >= 70 THEN print PASS ELSE print FAIL. Only one branch runs.
- Input / Output: INPUT reads a value from the user. PRINT (or OUTPUT) shows a value on the screen.
Step-by-step: a pass/fail program
- INPUT score (the user types a number, say 85).
- IF score >= 70 THEN print PASS ELSE print FAIL.
- Trace it: 85 >= 70 is TRUE, so the program prints PASS.
- Trace it again with score = 60: 60 >= 70 is FALSE, so the program prints FAIL.
Building a simple calculator
A calculator program takes two numbers and an operator, then prints the result. In pseudocode: INPUT a, INPUT b, INPUT op. IF op is plus THEN result = a + b. IF op is minus THEN result = a - b. IF op is times THEN result = a * b. IF op is divide THEN result = a / b. PRINT result. With a = 12, b = 8, op is plus, the result is 20.
What does this program print? score = 92. IF score >= 90 THEN print A ELSE IF score >= 80 THEN print B ELSE print C.
92 >= 90 is TRUE, so the first branch runs and the program prints A. The other branches are skipped because only one branch of an IF/ELSE chain runs.
Building a math quiz
- Pick a question (for example: What is 7 x 8 ?) and store the correct answer: correct = 56.
- Show the question and read the user reply: PRINT question, INPUT guess.
- Compare with a conditional: IF guess = correct THEN print Right! ELSE print Try again.
- Track score across many questions with a counter: score = score + 1 every time the user is right.
Tip: when you trace a program, write each variable in a small table and update its value line by line. Watching the values change is the fastest way to spot a bug.
Unlock the full HSEA system for 7 days.
Add your first kid profile and your 7 days start: every lesson in every grade, saved progress for each child. Nothing to set up, no card required, nothing to cancel.
- ✓ All 1,680 lessons, Grades 1–6, every subject
- ✓ Profiles + saved progress for every kid
- ✓ Printable lessons with answer key