Code Guide: Basic Arithmetic Operations And Calculations (Copy)
Basic Arithmetic Operations And Calculations
Core Arithmetic Rules (Exam-Critical)
- All calculations must use assigned variables
- Use
←for assignment, never= - Order of operations matters:
- Brackets first
- Multiplication / Division
- Addition / Subtraction
- Choose correct data type:
- INTEGER for whole-number results
- REAL for divisions, averages, percentages
Arithmetic Operators (Must Memorise)
| Operation | Operator | Example |
|---|---|---|
| Addition | + | sum ← a + b |
| Subtraction | – | diff ← a - b |
| Multiplication | * | area ← l * w |
| Division | / | avg ← total / count |
| Remainder | MOD | IF n MOD 2 = 0 THEN |
Example 1: Addition And Subtraction
Task
- Input two integers
- Output their sum and difference
DECLARE a : INTEGER
DECLARE b : INTEGER
DECLARE sum : INTEGER
DECLARE diff : INTEGER
INPUT a
INPUT b
sum ← a + b
diff ← a - b
OUTPUT sum
OUTPUT diff
Revision Point
- Subtraction is order-sensitive
a - b≠b - a
Example 2: Multiplication (Total Cost)
Task
- Input quantity and price
- Output total cost
DECLARE quantity : INTEGER
DECLARE price : REAL
DECLARE totalCost : REAL
INPUT quantity
INPUT price
totalCost ← quantity * price
OUTPUT totalCost
Revision Point
- If one value is REAL, result must be REAL
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Example 3: Division And Averages
Task
- Input 5 numbers
- Output their average
DECLARE total : REAL
DECLARE num : REAL
total ← 0
FOR i ← 1 TO 5
INPUT num
total ← total + num
NEXT i
average ← total / 5
OUTPUT average
Revision Point
- Average always uses division
- Use REAL to avoid truncation
Example 4: Percentage Calculation
Task
- Input obtained marks and total marks
- Output percentage
DECLARE obtained : REAL
DECLARE totalMarks : REAL
DECLARE percentage : REAL
INPUT obtained
INPUT totalMarks
percentage ← (obtained / totalMarks) * 100
OUTPUT percentage
Revision Point
- Brackets are mandatory
- Without brackets, calculation is wrong
Example 5: Discount Calculation
Task
- Input price
- Apply 15% discount
- Output final price
DECLARE price : REAL
DECLARE discount : REAL
DECLARE finalPrice : REAL
INPUT price
discount ← price * 0.15
finalPrice ← price - discount
OUTPUT finalPrice
Revision Point
- Percentage converted to decimal
- Never subtract percentage directly
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Example 6: Combined Arithmetic Expression
Task
- Calculate result of
(a + b) × (c − d)
DECLARE a : INTEGER
DECLARE b : INTEGER
DECLARE c : INTEGER
DECLARE d : INTEGER
DECLARE result : INTEGER
INPUT a
INPUT b
INPUT c
INPUT d
result ← (a + b) * (c - d)
OUTPUT result
Revision Point
- Missing brackets = wrong logic
- Examiner penalises silently
Example 7: Temperature Conversion
Task
- Convert Celsius to Fahrenheit
- Formula:
F = (C × 9 / 5) + 32
DECLARE c : REAL
DECLARE f : REAL
INPUT c
f ← (c * 9 / 5) + 32
OUTPUT f
Revision Point
- Use REAL
- Preserve order exactly
Example 8: Integer Division Vs Real Division
Case A: INTEGER Division (Wrong For Average)
DECLARE total : INTEGER
DECLARE avg : INTEGER
total ← 7
avg ← total / 2
OUTPUT avg
Case B: REAL Division (Correct)
DECLARE total : REAL
DECLARE avg : REAL
total ← 7
avg ← total / 2
OUTPUT avg
Revision Point
- INTEGER division loses decimals
- Always upgrade to REAL when dividing
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Example 9: MOD Operator Applications
Task A: Even Or Odd Check
INPUT num
IF num MOD 2 = 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
ENDIF
Task B: Multiples Of 5
INPUT num
IF num MOD 5 = 0 THEN
OUTPUT "Multiple of 5"
ENDIF
Revision Point
- MOD only works on INTEGER
- Used for divisibility checks
Example 10: Accumulation With Arithmetic
Task
- Input 6 values
- Output their total and average
DECLARE total : REAL
DECLARE num : REAL
total ← 0
FOR i ← 1 TO 6
INPUT num
total ← total + num
NEXT i
average ← total / 6
OUTPUT total
OUTPUT average
Revision Point
- Accumulator pattern = exam favourite
- Initialise before loop
Common Arithmetic Mistakes (High-Frequency)
Mistake 1: Missing Initialisation
- Wrong:
total ← total + num
- Correct:
total ← 0
total ← total + num
Mistake 2: Wrong Bracket Placement
- Wrong:
percentage ← obtained / total * 100
- Correct:
percentage ← (obtained / total) * 100
Mistake 3: Integer Used Where REAL Needed
- Averages
- Percentages
- Currency
Mistake 4: Outputting Wrong Variable
- Calculated
finalPrice - Outputting
priceby mistake
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Exam-Safe Arithmetic Templates (Copy-Paste Ready)
Template 1: Sum And Average
total ← 0
FOR i ← 1 TO n
INPUT value
total ← total + value
NEXT i
average ← total / n
OUTPUT average
Template 2: Percentage
percentage ← (part / whole) * 100
OUTPUT percentage
Template 3: Price After Discount
discount ← price * rate
final ← price - discount
OUTPUT final
Template 4: Even/Odd Using MOD
IF num MOD 2 = 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
ENDIF
Final Arithmetic Checklist (Before You Move On)
- All variables initialised?
- Correct data types chosen?
- Brackets used where needed?
- Division handled using REAL?
- Correct variable output?
If yes — full marks territory.
