Operators

Master arithmetic, logical, and comparison operators with 10 hands-on exercises

3 Easy 4 Medium 3 Hard ~75 min total
Related Lesson
Operators Theory
1

Basic Calculator

Easy 6 min

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:

Sample Run
Enter first number: 10 Enter second number: 3 10 + 3 = 13 10 - 3 = 7 10 × 3 = 30 10 ÷ 3 = 3.33
Need Help? Try These Hints First!

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
Key Concepts:
  • Arithmetic operators: +, -, *, /
  • Storing calculation results in variables
  • Order of operations matters
  • Division returns decimal results
2

Modulo Operations

Easy 5 min

Problem:

Use the modulo operator (mod) to determine if a number is even or odd.

Example Output:

Sample Run
Enter a number: 7 7 mod 2 = 1 The number is odd
Need Help? Try These Hints First!

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
Key Concepts:
  • Modulo (mod) returns remainder
  • Even numbers: n mod 2 = 0
  • Odd numbers: n mod 2 = 1
3

Comparison Operators

Easy 6 min

Problem:

Compare two numbers using all comparison operators (==, !=, <, >, <=, >=).

Sample Run
Enter first number: 5 Enter second number: 8 5 == 8: False 5 != 8: True 5 < 8: True 5 > 8: False 5 <= 8: True 5 >= 8: False
Need Help? Try These Hints First!

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
Key Concepts:
  • Comparison operators return True/False
  • == (equals), != (not equals)
  • <, >, <=, >= for numerical comparison
4

Logical Operators

Medium 8 min

Problem:

Check if a student passes based on attendance AND grade using logical operators.

Sample Run
Enter attendance %: 85 Enter grade: 72 Attendance >= 75: True Grade >= 60: True Passes course (AND): True
Need Help? Try These Hints First!

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
Key Concepts:
  • Logical AND: both conditions must be true
  • Boolean variables store True/False
  • Combining multiple conditions
5

Percentage Calculator

Medium 7 min

Problem:

Calculate what percentage one number is of another.

Sample Run
Enter part: 25 Enter whole: 200 25 is 12.5% of 200
Need Help? Try These Hints First!

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
Key Concepts:
  • Percentage formula: (part / whole) × 100
  • Division and multiplication operators
  • Order of operations important
6

Discount Calculator

Medium 8 min

Problem:

Calculate final price after applying a discount percentage.

Sample Run
Enter original price: 100 Enter discount %: 20 Discount amount: 20.0 Final price: 80.0
Need Help? Try These Hints First!

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
Key Concepts:
  • Percentage to decimal: divide by 100
  • Subtraction operator for reduction
  • Multiple operations in sequence
7

Power Calculator

Medium 7 min

Problem:

Calculate a number raised to a power (e.g., 2^3 = 2 × 2 × 2 = 8).

Sample Run
Enter base: 2 Enter exponent: 3 2^3 = 8
Need Help? Try These Hints First!

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
Key Concepts:
  • Exponentiation using repeated multiplication
  • Multiplication operator (*)
  • Loop would be better for variable exponents
8

Average of Three Numbers

Medium 6 min

Problem:

Calculate the average of three numbers.

Sample Run
Enter number 1: 10 Enter number 2: 20 Enter number 3: 30 Average: 20.0
Need Help? Try These Hints First!

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
Key Concepts:
  • Addition combines multiple values
  • Division for average calculation
  • Parentheses control operation order
9

Swap Two Values

Hard 10 min

Problem:

Swap the values of two variables using a temporary variable.

Sample Run
Enter first value: 5 Enter second value: 10 Before swap: a=5, b=10 After swap: a=10, b=5
Need Help? Try These Hints First!

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
Key Concepts:
  • Assignment operator (=) changes variable values
  • Temporary variable needed to preserve values
  • Order of assignments matters
10

Complex Expression

Hard 12 min

Problem:

Evaluate a complex mathematical expression: result = (a + b) * c - d / e

Sample Run
Enter a: 5 Enter b: 3 Enter c: 2 Enter d: 10 Enter e: 2 (5 + 3) * 2 - 10 / 2 = 11.0
Need Help? Try These Hints First!

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
Key Concepts:
  • Order of operations: parentheses first
  • Then multiplication and division (left to right)
  • Finally addition and subtraction
  • Complex expressions with multiple operators