📖 Scenario: You are helping a small shop owner calculate the total cost of items bought by customers. You will use arithmetic operators to add, subtract, multiply, and divide prices and quantities.
🎯 Goal: Build a simple MATLAB script that uses arithmetic operators to calculate the total price, discount, and final amount to pay.
📋 What You'll Learn
Create variables with exact names and values for prices and quantities
Use arithmetic operators: addition (+), subtraction (-), multiplication (*), and division (/)
Calculate total cost before discount
Calculate discount amount using a discount rate
Calculate final amount after discount
Print the final amount
💡 Why This Matters
🌍 Real World
Calculating prices and discounts is common in shops and online stores to help customers know how much to pay.
💼 Career
Understanding arithmetic operations and variables is essential for programming tasks in data analysis, finance, and software development.
Progress0 / 4 steps
1
Create item prices and quantities
Create variables called priceApple with value 3, priceBanana with value 2, qtyApple with value 5, and qtyBanana with value 7.
MATLAB
Hint
Use the assignment operator = to set values to variables.
2
Set discount rate
Create a variable called discountRate and set it to 0.1 (which means 10%).
MATLAB
Hint
Discount rate is a decimal number representing percentage.
3
Calculate total cost and discount
Calculate the total cost before discount in a variable called totalCost by multiplying prices and quantities and adding them. Then calculate the discount amount in a variable called discountAmount by multiplying totalCost by discountRate.
MATLAB
Hint
Use * for multiplication and + for addition.
4
Calculate final amount and display
Calculate the final amount to pay in a variable called finalAmount by subtracting discountAmount from totalCost. Then print the finalAmount using disp.
MATLAB
Hint
Use - for subtraction and disp to show the result.
Practice
(1/5)
1. Which arithmetic operator in MATLAB is used to multiply two numbers?
easy
A. +
B. -
C. *
D. ^
Solution
Step 1: Identify multiplication operator
In MATLAB, the symbol * is used to multiply two numbers.
Step 2: Confirm other operators
+ is addition, - is subtraction, and ^ is power.
Final Answer:
* -> Option C
Quick Check:
Multiplication = * [OK]
Hint: Remember * means multiply in math and MATLAB [OK]
Common Mistakes:
Confusing * with + or -
Using ^ for multiplication
Forgetting * is needed for multiply
2. Which of the following is the correct MATLAB syntax using the power operator to calculate 5 raised to the power 3?
easy
A. 5^3
B. 5**3
C. power(5,3)
D. 5^^3
Solution
Step 1: Identify power operator syntax
In MATLAB, the caret symbol ^ is used for powers, so 5^3 is correct.
Step 2: Check other options
5**3 and 5^^3 are invalid syntax. power(5,3) is a function but not an operator.
Final Answer:
5^3 -> Option A
Quick Check:
Power operator = ^ [OK]
Hint: Use ^ for powers, not ** or ^^ [OK]
Common Mistakes:
Using ** like other languages
Typing ^^ by mistake
Confusing function with operator
3. What is the output of the following MATLAB code?
a = 10; b = 3; c = a / b;
medium
A. 3.3333
B. 3
C. 7
D. 13
Solution
Step 1: Understand division operation
Variable a is 10, b is 3, so c = a / b divides 10 by 3.
Step 2: Calculate division result
10 divided by 3 equals approximately 3.3333 in MATLAB.
Final Answer:
3.3333 -> Option A
Quick Check:
10 / 3 = 3.3333 [OK]
Hint: Division gives decimal result unless integers used carefully [OK]
Common Mistakes:
Rounding to 3
Adding instead of dividing
Confusing division with subtraction
4. Find the error in this MATLAB expression:
result = 4 + * 5;
medium
A. Wrong operator for addition
B. Missing operand before *
C. Extra semicolon
D. No error
Solution
Step 1: Analyze the expression syntax
The expression 4 + * 5 has two operators + and * without a number between them.
Step 2: Identify missing operand
There should be a number between + and *, but it is missing, causing a syntax error.
Final Answer:
Missing operand before * -> Option B
Quick Check:
Operators need numbers between them [OK]
Hint: Operators must have numbers on both sides [OK]
Common Mistakes:
Leaving out numbers between operators
Thinking semicolon causes error
Using wrong operator symbols
5. You want to calculate the expression (2 + 3) ^ 2 - 4 * 5 in MATLAB. Which code correctly computes this?
hard
A. 2 + (3 ^ 2 - 4) * 5
B. 2 + 3 ^ 2 - 4 * 5
C. (2 + 3) ^ (2 - 4) * 5
D. (2 + 3) ^ 2 - 4 * 5
Solution
Step 1: Understand operator precedence and parentheses
The expression (2 + 3) ^ 2 - 4 * 5 means add 2 and 3 first, then square, then subtract 4 times 5.
Step 2: Check each option
(2 + 3) ^ 2 - 4 * 5 matches the original expression exactly. Others change order or grouping, giving different results.
Final Answer:
(2 + 3) ^ 2 - 4 * 5 -> Option D
Quick Check:
Use parentheses to keep order correct [OK]
Hint: Use parentheses to control math order [OK]
Common Mistakes:
Ignoring parentheses
Changing operator order
Misplacing powers or multiplication
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.