Table of Contents

    Practice Assignment: Build a Simple Calculator

    Programming Mastery

    Practice Assignment: Build a Simple Calculator

    Apply input, output, variables, data types, operators, expressions, conditional logic, basic input validation, and formatted output by building a simple calculator using language-neutral pseudocode.

    Assignment Overview

    In this practice assignment, students will build a simple calculator that accepts two numbers and an arithmetic operation from the user, performs the selected calculation, and displays the result clearly.

    This assignment is designed to help students combine several beginner programming concepts in one practical mini project. Students will practice reading user data, validating input, using arithmetic operators, applying conditions, handling invalid choices, avoiding division by zero, and formatting output properly.

    A simple calculator is one of the best beginner projects because it connects input, processing, and output in a clear and practical way.

    Learning Objectives

    After completing this assignment, students should be able to:

    Objectives

    • Read numeric input from the user.
    • Read an operation choice from the user.
    • Store input values in meaningful variables.
    • Convert input values to suitable numeric data types.
    • Use arithmetic operators for calculation.
    • Use conditional statements to select the correct operation.
    • Validate user input before processing.
    • Handle invalid operation choices.
    • Prevent division by zero errors.
    • Display output in a clear and formatted way.
    • Write clean and readable language-neutral pseudocode.

    Prerequisites

    Before attempting this assignment, students should revise the following topics:

    Required Topics

    • What is input?
    • What is output?
    • Console input and console output.
    • Reading user data.
    • Formatting output.
    • Basic input validation.
    • Variables and data types.
    • Type conversion and type casting.
    • Arithmetic operators.
    • Assignment operators.
    • Expressions.
    • Conditional statements.

    Assignment Instructions

    Build a simple calculator using language-neutral pseudocode. The calculator should ask the user for two numbers and one operation. Based on the selected operation, the program should calculate and display the result.

    Important: This assignment is not specific to Java, Python, C, C++, or any single programming language. Students should write the solution using clear language-neutral pseudocode.

    General Instructions

    • Use meaningful variable names.
    • Use prompts before reading input.
    • Read two numbers from the user.
    • Read the operation choice from the user.
    • Support addition, subtraction, multiplication, and division.
    • Validate numeric input where possible.
    • Validate the operation choice.
    • Do not allow division by zero.
    • Display the result with a clear label.
    • Use proper indentation and readable structure.

    Calculator Requirements

    Your calculator program must satisfy the following requirements.

    Requirement Description
    Input First Number The program should ask the user to enter the first number.
    Input Second Number The program should ask the user to enter the second number.
    Input Operation The program should ask the user to choose an operation.
    Addition The calculator should add two numbers.
    Subtraction The calculator should subtract the second number from the first number.
    Multiplication The calculator should multiply two numbers.
    Division The calculator should divide the first number by the second number.
    Division by Zero Check The calculator should not divide by zero.
    Invalid Operation Check The calculator should show an error message for invalid operation choice.
    Formatted Output The result should be displayed clearly with labels.

    Input → Process → Output Plan

    Before writing pseudocode, students should understand the calculator using the IPO model.

    Stage Details Example
    Input Read first number, second number, and operation. 10, 5, +
    Process Perform the selected arithmetic operation. 10 + 5
    Output Display the calculated result. Result: 15

    Expected User Inputs

    The calculator should read the following values from the user:

    • First number: A numeric value.
    • Second number: A numeric value.
    • Operation: One of the allowed operation symbols or menu choices.

    Example Input

    First number: 20
    Second number: 5
    Operation: /

    Expected Output

    Result: 20 / 5 = 4

    Operation Menu

    Students may use either operation symbols or menu numbers. Both approaches are acceptable.

    Option A: Symbol-Based Menu

    Choose operation:
    + for Addition
    - for Subtraction
    * for Multiplication
    / for Division

    Option B: Number-Based Menu

    Choose operation:
    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    Teacher Note: For beginners, number-based menu choices are often easier to validate. Symbol-based choices help students understand operators more directly.

    Validation Rules

    The calculator should apply basic input validation.

    Input Validation Rule Error Message
    First Number Must be a valid number. Please enter a valid first number.
    Second Number Must be a valid number. Please enter a valid second number.
    Operation Must be one of the allowed operations. Invalid operation selected.
    Division Second number must not be zero. Division by zero is not allowed.

    Task 1: Write the Algorithm

    Write an algorithm for building a simple calculator.

    Expected Algorithm Structure

    Step 1: Start
    Step 2: Ask the user to enter the first number
    Step 3: Read the first number
    Step 4: Ask the user to enter the second number
    Step 5: Read the second number
    Step 6: Display operation menu
    Step 7: Read operation choice
    Step 8: If operation is addition, calculate sum
    Step 9: If operation is subtraction, calculate difference
    Step 10: If operation is multiplication, calculate product
    Step 11: If operation is division, check second number is not zero
    Step 12: If valid, calculate quotient
    Step 13: Display result
    Step 14: Stop

    Task 2: Write Language-Neutral Pseudocode

    Write pseudocode for the calculator using clear and readable steps.

    Your Pseudocode Area

    WRITE YOUR SIMPLE CALCULATOR PSEUDOCODE HERE

    Task 3: Create a Trace Table

    Use the following test case and complete the trace table.

    Test Case

    First Number = 12
    Second Number = 4
    Operation = *
    Step Variable / Action Value
    1 firstNumber ________
    2 secondNumber ________
    3 operation ________
    4 result ________
    5 Output ________

    Task 4: Test Your Calculator

    Test your calculator using the following test cases.

    Test Case First Number Second Number Operation Expected Output
    1 10 5 + 15
    2 10 5 - 5
    3 10 5 * 50
    4 10 5 / 2
    5 10 0 / Division by zero is not allowed
    6 10 5 % Invalid operation selected

    Sample Solution: Algorithm

    Step 1: Start
    Step 2: Declare firstNumber, secondNumber, operation, and result
    Step 3: Ask the user to enter the first number
    Step 4: Read firstNumber
    Step 5: Ask the user to enter the second number
    Step 6: Read secondNumber
    Step 7: Display operation choices
    Step 8: Read operation
    Step 9: If operation is "+", add firstNumber and secondNumber
    Step 10: Else if operation is "-", subtract secondNumber from firstNumber
    Step 11: Else if operation is "*", multiply firstNumber and secondNumber
    Step 12: Else if operation is "/", check whether secondNumber is 0
    Step 13: If secondNumber is 0, display division by zero error
    Step 14: Otherwise divide firstNumber by secondNumber
    Step 15: If operation is invalid, display invalid operation message
    Step 16: Display result if calculation is successful
    Step 17: Stop

    Sample Solution: Basic Calculator Pseudocode

    /*
    Practice Assignment: Build a Simple Calculator
    This program accepts two numbers and an operation,
    then displays the calculated result.
    */
    
    ENTRY POINT
        DECLARE firstNumber AS DECIMAL = 0.0
        DECLARE secondNumber AS DECIMAL = 0.0
        DECLARE operation AS TEXT = ""
        DECLARE result AS DECIMAL = 0.0
        DECLARE isCalculationSuccessful AS BOOLEAN = true
    
        DISPLAY "----- Simple Calculator -----"
    
        DISPLAY "Enter first number:"
        INPUT firstNumber
    
        DISPLAY "Enter second number:"
        INPUT secondNumber
    
        DISPLAY "Choose operation:"
        DISPLAY "+ for Addition"
        DISPLAY "- for Subtraction"
        DISPLAY "* for Multiplication"
        DISPLAY "/ for Division"
    
        INPUT operation
    
        IF operation == "+" THEN
            SET result = firstNumber + secondNumber
    
        ELSE IF operation == "-" THEN
            SET result = firstNumber - secondNumber
    
        ELSE IF operation == "*" THEN
            SET result = firstNumber * secondNumber
    
        ELSE IF operation == "/" THEN
            IF secondNumber == 0 THEN
                DISPLAY "Error: Division by zero is not allowed"
                SET isCalculationSuccessful = false
            ELSE
                SET result = firstNumber / secondNumber
            END IF
    
        ELSE
            DISPLAY "Error: Invalid operation selected"
            SET isCalculationSuccessful = false
        END IF
    
        IF isCalculationSuccessful == true THEN
            DISPLAY "----- Result -----"
            DISPLAY "First Number  : " + firstNumber
            DISPLAY "Second Number : " + secondNumber
            DISPLAY "Operation     : " + operation
            DISPLAY "Result        : " + result
        END IF
    END ENTRY POINT

    Sample Solution with Basic Input Validation

    The following version includes simple validation ideas. It checks whether numeric input can be converted and whether the operation is valid.

    /*
    Calculator with basic input validation.
    */
    
    ENTRY POINT
        DECLARE firstNumberText AS TEXT = ""
        DECLARE secondNumberText AS TEXT = ""
        DECLARE operation AS TEXT = ""
        DECLARE firstNumber AS DECIMAL = 0.0
        DECLARE secondNumber AS DECIMAL = 0.0
        DECLARE result AS DECIMAL = 0.0
        DECLARE isValid AS BOOLEAN = true
    
        DISPLAY "----- Simple Calculator -----"
    
        DISPLAY "Enter first number:"
        INPUT firstNumberText
    
        DISPLAY "Enter second number:"
        INPUT secondNumberText
    
        IF firstNumberText CANNOT BE CONVERTED TO DECIMAL THEN
            DISPLAY "Error: First number must be numeric"
            SET isValid = false
        ELSE IF secondNumberText CANNOT BE CONVERTED TO DECIMAL THEN
            DISPLAY "Error: Second number must be numeric"
            SET isValid = false
        ELSE
            SET firstNumber = CONVERT firstNumberText TO DECIMAL
            SET secondNumber = CONVERT secondNumberText TO DECIMAL
        END IF
    
        IF isValid == true THEN
            DISPLAY "Choose operation:"
            DISPLAY "+ Addition"
            DISPLAY "- Subtraction"
            DISPLAY "* Multiplication"
            DISPLAY "/ Division"
    
            INPUT operation
    
            IF operation == "+" THEN
                SET result = firstNumber + secondNumber
    
            ELSE IF operation == "-" THEN
                SET result = firstNumber - secondNumber
    
            ELSE IF operation == "*" THEN
                SET result = firstNumber * secondNumber
    
            ELSE IF operation == "/" THEN
                IF secondNumber == 0 THEN
                    DISPLAY "Error: Division by zero is not allowed"
                    SET isValid = false
                ELSE
                    SET result = firstNumber / secondNumber
                END IF
    
            ELSE
                DISPLAY "Error: Invalid operation selected"
                SET isValid = false
            END IF
        END IF
    
        IF isValid == true THEN
            DISPLAY "----- Calculation Summary -----"
            DISPLAY "First Number  : " + firstNumber
            DISPLAY "Second Number : " + secondNumber
            DISPLAY "Operation     : " + operation
            DISPLAY "Result        : " + FORMAT_DECIMAL(result, 2)
        END IF
    END ENTRY POINT

    Bonus Task: Repeat Calculator Until User Exits

    As a bonus challenge, modify the calculator so the user can perform multiple calculations without restarting the program.

    Bonus Requirements

    • Ask the user whether they want to perform another calculation.
    • If the user enters yes, repeat the calculator.
    • If the user enters no, end the program.
    • Validate the yes/no choice.
    • Display a thank-you message before ending.

    Bonus Pseudocode Idea

    SET continueChoice = "yes"
    
    WHILE continueChoice == "yes" DO
        PERFORM calculator steps
    
        DISPLAY "Do you want to calculate again? yes/no"
        INPUT continueChoice
    END WHILE
    
    DISPLAY "Thank you for using the calculator"

    Bonus Task: Add More Operations

    Students who finish early can extend the calculator by adding more operations.

    Optional Extra Features

    • Add modulus or remainder operation.
    • Add square of a number.
    • Add average of two numbers.
    • Add percentage calculation.
    • Add maximum of two numbers.
    • Add minimum of two numbers.
    • Add menu-based operation selection.
    • Add repeated input until valid values are entered.

    Common Mistakes to Avoid

    Mistakes

    • Not asking the user for input clearly.
    • Using unclear variable names like a, b, and x.
    • Forgetting to check invalid operation choices.
    • Dividing by zero without checking.
    • Displaying only the result without labels.
    • Mixing input, processing, and output in a confusing way.
    • Not formatting output properly.
    • Not testing all operations.

    Better Habits

    • Use clear prompts before input.
    • Use meaningful names like firstNumber, secondNumber, and operation.
    • Validate operation choice.
    • Check division by zero.
    • Use labels in output.
    • Keep the program structure clean.
    • Format output clearly.
    • Test normal and invalid cases.

    Submission Checklist

    Before submitting, students should verify the following:

    Checklist

    • The program accepts two numbers.
    • The program accepts an operation choice.
    • Addition works correctly.
    • Subtraction works correctly.
    • Multiplication works correctly.
    • Division works correctly.
    • Division by zero is handled.
    • Invalid operation is handled.
    • Output is clearly formatted.
    • Variables have meaningful names.
    • Pseudocode is properly indented.
    • At least six test cases are completed.

    Evaluation Rubric

    The assignment can be evaluated using the following rubric.

    Criteria Marks What to Check
    Input Handling 15 Correctly reads two numbers and operation choice.
    Arithmetic Operations 25 Correctly performs addition, subtraction, multiplication, and division.
    Conditional Logic 15 Uses conditions properly to select operation.
    Validation 15 Handles invalid operation and division by zero.
    Output Formatting 10 Displays result clearly with labels.
    Code Readability 10 Uses meaningful names and proper indentation.
    Testing 10 Includes correct test cases and expected outputs.

    Sample Answer: Trace Table

    For the test case 12, 4, and *, the trace table should look like this:

    Step Variable / Action Value
    1 firstNumber 12
    2 secondNumber 4
    3 operation *
    4 result = firstNumber * secondNumber 48
    5 Output Result: 12 * 4 = 48

    Mini Viva Questions

    1

    Why do we need input in a calculator program?

    Input allows the user to enter numbers and select the operation while the program is running.

    2

    Why should division by zero be checked?

    Division by zero is not mathematically valid and can cause program errors, so it should be prevented.

    3

    Which operators are used in a simple calculator?

    Common operators are addition +, subtraction -, multiplication *, and division /.

    4

    Why is output formatting important?

    Output formatting helps users understand the calculation result clearly.

    5

    What is the role of conditional statements in this assignment?

    Conditional statements help the program decide which arithmetic operation should be performed.

    Quick Summary

    Concept How It Is Used in Calculator
    Input User enters two numbers and operation choice.
    Variables Store numbers, operation, and result.
    Operators Perform arithmetic calculations.
    Expressions Combine variables and operators to calculate result.
    Conditional Statements Select the correct operation.
    Validation Handle invalid operation and division by zero.
    Output Display calculation result clearly.

    Final Takeaway

    The Simple Calculator assignment helps students apply multiple programming fundamentals in one practical project. It connects input, variables, data types, operators, expressions, conditional logic, validation, and formatted output. By completing this assignment, students move from learning individual concepts to building a small working program using structured problem-solving.