Operators
Master arithmetic, logical, and comparison operators with 10 hands-on exercises
Basic Calculator
Problem:
Create a simple calculator that performs all four basic arithmetic operations (addition, subtraction, multiplication, division) on two numbers.
Requirements:
- Get two numbers from the user
- Calculate and display all four operations
- Use proper arithmetic operators (+, -, *, /)
Example Output:
Solution
Algorithm BasicCalculator
var num1 = Input "Enter first number:"
var num2 = Input "Enter second number:"
# Perform all four operations
var sum = num1 + num2
var difference = num1 - num2
var product = num1 * num2
var quotient = num1 / num2
Print num1, "+", num2, "=", sum
Print num1, "-", num2, "=", difference
Print num1, "×", num2, "=", product
Print num1, "÷", num2, "=", quotient
Endalgorithm
- Arithmetic operators: +, -, *, /
- Storing calculation results in variables
- Order of operations matters
- Division returns decimal results
Modulo Operations
Problem:
Use the modulo operator (mod) to determine if a number is even or odd.
Example Output:
Solution
Algorithm EvenOddChecker
var number = Input "Enter a number:"
var remainder = number mod 2
Print number, "mod 2 =", remainder
If remainder == 0 Then
Print "The number is even"
Else
Print "The number is odd"
Endif
Endalgorithm
- Modulo (mod) returns remainder
- Even numbers: n mod 2 = 0
- Odd numbers: n mod 2 = 1
Comparison Operators
Problem:
Compare two numbers using all comparison operators (==, !=, <, >, <=, >=).
Solution
Algorithm ComparisonDemo
var a = Input "Enter first number:"
var b = Input "Enter second number:"
Print a, "== ", b, ":", (a == b)
Print a, "!= ", b, ":", (a != b)
Print a, "< ", b, ":", (a < b)
Print a, "> ", b, ":", (a > b)
Print a, "<= ", b, ":", (a <= b)
Print a, ">= ", b, ":", (a >= b)
Endalgorithm
- Comparison operators return True/False
- == (equals), != (not equals)
- <, >, <=, >= for numerical comparison
Logical Operators
Problem:
Check if a student passes based on attendance AND grade using logical operators.
Solution
Algorithm PassChecker
var attendance = Input "Enter attendance %:"
var grade = Input "Enter grade:"
var goodAttendance = attendance >= 75
var passingGrade = grade >= 60
Print "Attendance >= 75:", goodAttendance
Print "Grade >= 60:", passingGrade
Print "Passes course (AND):", (goodAttendance AND passingGrade)
Endalgorithm
- Logical AND: both conditions must be true
- Boolean variables store True/False
- Combining multiple conditions
Percentage Calculator
Problem:
Calculate what percentage one number is of another.
Solution
Algorithm PercentageCalculator
var part = Input "Enter part:"
var whole = Input "Enter whole:"
var percentage = (part / whole) * 100
Print part, "is", percentage, "% of", whole
Endalgorithm
- Percentage formula: (part / whole) × 100
- Division and multiplication operators
- Order of operations important
Discount Calculator
Problem:
Calculate final price after applying a discount percentage.
Solution
Algorithm DiscountCalculator
var price = Input "Enter original price:"
var discountPercent = Input "Enter discount %:"
var discountAmount = price * (discountPercent / 100)
var finalPrice = price - discountAmount
Print "Discount amount:", discountAmount
Print "Final price:", finalPrice
Endalgorithm
- Percentage to decimal: divide by 100
- Subtraction operator for reduction
- Multiple operations in sequence
Power Calculator
Problem:
Calculate a number raised to a power (e.g., 2^3 = 2 × 2 × 2 = 8).
Solution
Algorithm PowerCalculator
var base = Input "Enter base:"
var exponent = Input "Enter exponent:"
var result = base * base * base # For exponent=3
Print base, "^", exponent, "=", result
Endalgorithm
- Exponentiation using repeated multiplication
- Multiplication operator (*)
- Loop would be better for variable exponents
Average of Three Numbers
Problem:
Calculate the average of three numbers.
Solution
Algorithm AverageCalculator
var num1 = Input "Enter number 1:"
var num2 = Input "Enter number 2:"
var num3 = Input "Enter number 3:"
var average = (num1 + num2 + num3) / 3
Print "Average:", average
Endalgorithm
- Addition combines multiple values
- Division for average calculation
- Parentheses control operation order
Swap Two Values
Problem:
Swap the values of two variables using a temporary variable.
Solution
Algorithm SwapValues
var a = Input "Enter first value:"
var b = Input "Enter second value:"
Print "Before swap: a=", a, ", b=", b
# Swap using temporary variable
var temp = a
a = b
b = temp
Print "After swap: a=", a, ", b=", b
Endalgorithm
- Assignment operator (=) changes variable values
- Temporary variable needed to preserve values
- Order of assignments matters
Complex Expression
Problem:
Evaluate a complex mathematical expression: result = (a + b) * c - d / e
Solution
Algorithm ComplexExpression
var a = Input "Enter a:"
var b = Input "Enter b:"
var c = Input "Enter c:"
var d = Input "Enter d:"
var e = Input "Enter e:"
var result = (a + b) * c - d / e
Print "(", a, "+", b, ") *", c, "-", d, "/", e, "=", result
Endalgorithm
- Order of operations: parentheses first
- Then multiplication and division (left to right)
- Finally addition and subtraction
- Complex expressions with multiple operators