🔢 MathGrade 6Lesson 37
← Back🔒 PDF with full access

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

  1. INPUT score (the user types a number, say 85).
  2. IF score >= 70 THEN print PASS ELSE print FAIL.
  3. Trace it: 85 >= 70 is TRUE, so the program prints PASS.
  4. 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.

Example

What does this program print? score = 92. IF score >= 90 THEN print A ELSE IF score >= 80 THEN print B ELSE print C.

Answer:A

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.
Pseudocode: Pass or FailA flow diagram showing INPUT score flowing into an IF score is greater than or equal to 70 decision. The TRUE branch leads to PRINT PASS; the FALSE branch leads to PRINT FAIL.Pseudocode: Pass or FailInput flows through a decision into one of two outputs.INPUT scoreIF score>= 70 ?TRUEFALSEPRINT PASSPRINT FAILif score >= 70 then print PASS else print FAILDecisions branch the program into different outputs.
A conditional picks one branch from many.
Calculator App MockupA small calculator showing the display 12 plus 8 equals 20, with a four by four number pad (digits 0 through 9, operators plus, minus, times, divide and equals) and side panels noting a=12, b=8, sum=a+b, and an OUTPUT of 20.Calculator App MockupRead input, do the math, show output.12 + 8 = 20789/456x123-0.=+a = 12b = 8sum = a+bOUTPUT20Variables hold values. Operators combine them.
A calculator turns inputs and operators into output.
💡

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.

Vocabulary
pseudocode
Plain-English code that shows the logic of a program without using a real programming language.
variable
A named storage box that holds a value the program can read or change.
expression
A formula that computes a value, such as (a + b) / 2.
conditional
An IF / ELSE statement that chooses one branch based on whether a test is true.
input
A value the user types in, which the program reads and stores in a variable.
output
A value the program shows on the screen using PRINT.
operator
A symbol such as +, -, x, or / that combines two values.
trace
To run a program by hand, line by line, writing down how each variable changes.
Free · 7 days · No credit card

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
Start your 7-day free trial →
← Lesson 36: Scale Drawings and MapsLesson 38: Create a Math Game →