🧮

Calculator

A calculator that handles + − × ÷, refuses to divide by zero, and remembers its history.

Project 1Beginner

🧩 Problem statement

Build a calculator. It takes two numbers and an operation and shows the result. Division by zero must give a friendly message. As an extension, keep a history of calculations.

📋 Requirements

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Friendly divide-by-zero message
  • History (extension)

📥 Inputs

  • A and B (numbers)
  • OP: "+", "-", "*" or "/"

📤 Outputs

  • The result, or a message

📏 Rules

  • Unknown operations show "Unknown operation"

🚧 Constraints

  • Numbers may be decimals

🧭 Suggested approach

  • Write CALCULATE(A, B, OP) with one branch per operation
  • Check B = 0 before dividing
  • The main program only takes input and displays the result

🧠 Required concepts

  • Variables
  • Conditions
  • Functions
✏️ Pseudocode editor · dry-run tool · test cases

Design your solution here. Use Run to dry-run it step by step and Run tests to check it. Hints unlock one at a time — try on your own first!

🧪 Edge cases to test

  • Division by zero
  • Unknown operation
  • Negative and decimal numbers

📈 Complexity analysis

Each calculation is O(1). A history of n calculations uses O(n) memory.

📖 Solution walkthrough (open after you’ve tried!)

Each operation gets its own branch inside one CALCULATE function. Division checks for zero before dividing, because dividing by zero is undefined. The final RETURN catches any operation we don’t support. The main program stays tiny: take the inputs, display the answer. For history, create an empty list before a loop and ADD each result TO it.

🚀 Challenge extensions

  • Add a history list and a “show history” command
  • Add power (^) and remainder (MOD)
  • Keep asking for calculations until the user types "quit"

← All projects