Post

Create a basic calculator that can add, subtract, multiply, and divide.

Create a basic calculator that can add, subtract, multiply, and divide.

{% raw %}

Building Blocks of the Calculator

  1. Operations: The calculator supports the following operations:
    • Addition
    • Subtraction
    • Multiplication
    • Division
    • Modulus
    • Exponentiation
  2. Functions: Each operation is encapsulated in its own function, making the code modular and reusable.
  3. User Interface: A simple text-based menu allows users to select an operation and input numbers.
  4. Input Handling: The program accepts user input for operation selection and the numbers to be used. It includes error handling for invalid inputs.
  5. Loop: The calculator runs in a loop, allowing multiple calculations until the user decides to quit.

Inputs and Outputs

Here’s how each part works with inputs and outputs:

Addition

  • Function: add(x, y)
  • Input: Two numbers, e.g., 10 and 5.
  • Output: The sum, e.g., 15.0.

Subtraction

  • Function: subtract(x, y)
  • Input: Two numbers, e.g., 10 and 5.
  • Output: The difference, e.g., 5.0.

Multiplication

  • Function: multiply(x, y)
  • Input: Two numbers, e.g., 10 and 5.
  • Output: The product, e.g., 50.0.

Division

  • Function: divide(x, y)
  • Input: Two numbers, e.g., 10 and 5.
  • Output: The quotient, e.g., 2.0.
  • Special Case: Division by zero returns an error message, e.g., "Error: Division by zero".

Modulus

  • Function: modulus(x, y)
  • Input: Two numbers, e.g., 10 and 5.
  • Output: The remainder, e.g., 0.0.

Exponentiation

  • Function: exponentiate(x, y)
  • Input: Two numbers, e.g., 10 and 2.
  • Output: The result of raising x to the power of y, e.g., 100.0.

User Interface Example

Below is an example session to illustrate how the user interacts with the calculator:

  • Menu Display:
Welcome to the basic calculator!Select operation:1. Add2. Subtract3. Multiply4. Divide5. Modulus6. Exponentiate
  • User Input
    • Operation Choice: Enter choice (1/2/3/4/5/6) or 'q' to quit: 1
    • First Number: Enter first number: 10
    • Second Number: Enter second number: 5
  • Output:
10.0 + 5.0 = 15.0
  • Repeat or Quit:
    • To perform another calculation, the user can select another operation.
    • To exit, the user can enter ‘q’.

Example Code Execution

The calculator continues to prompt for input until the user decides to quit. Below is a sample run with various operations:

Welcome to the basic calculator!Select operation:1. Add2. Subtract3. Multiply4. Divide5. Modulus6. ExponentiateEnter choice (1/2/3/4/5/6) or 'q' to quit: 3Enter first number: 7Enter second number: 87.0 * 8.0 = 56.0Enter choice (1/2/3/4/5/6) or 'q' to quit: 4Enter first number: 10Enter second number: 0Error: Division by zeroEnter choice (1/2/3/4/5/6) or 'q' to quit: 6Enter first number: 2Enter second number: 32.0 ^ 3.0 = 8.0Enter choice (1/2/3/4/5/6) or 'q' to quit: qExiting calculator. Goodbye!

This simple calculator provides a foundation that you can extend with more advanced features, such as handling complex numbers, adding more mathematical functions, or creating a graphical user interface (GUI).

{% endraw %}

This post is licensed under CC BY 4.0 by the author.