1 / 18
Session 6: Arithmetic Operations & Math Magic
๐งฎ Making Computers Do Math for Us!
Today we turn our computer into a super-powered calculator!
From basic operations to complex mathematical expressions
โจ Let computers handle the hard math! โจ
๐ฏ Today's Mathematical Mission
โ
Master Basic Operators
+, -, *, /, %
๐
Order of Operations
PEMDAS in programming
๐งฎ
Mathematical Expressions
Complex calculations
๐พ
Store Results
Save calculations in variables
๐ฎ
Build Calculator
Your own math application
๐ฌ
Real-World Problems
Solve practical math
๐ Quick Input/Output Review!
๐ค What function gets user input?
scanf()
๐จ What function displays output?
printf()
๐ข What symbol do we need before variable in scanf?
& (ampersand)
๐ What format specifier is used for integers?
%d
๐ Interactive Programs Showcase!
๐ Let's see your amazing creations!
๐งฎ Personal Calculator: Show us your calculator with name personalization!
๐ญ "About Me" Interview: Demonstrate your 5-question profile program!
๐ข Favorite Numbers: Display your average calculator!
๐จ printf() Experiments: Share your coolest formatting discoveries!
๐ญ Creative Ideas: What interesting questions did you ask users?
๐ Time to share and learn from each other!
๐ค Why Computers Are Math Superheroes
โก Lightning Fast
Billions of calculations per second
Try beating that with a calculator!
๐ฏ Perfect Accuracy
Never makes arithmetic mistakes
No more "oops, wrong button!"
๐ Repeatable
Same calculation, same result every time
Consistency is key!
๐ Complex Operations
Handles huge numbers and complex formulas
From simple addition to rocket science!
๐ Today we harness this mathematical power!
๐ฆธโโ๏ธ Meet the Arithmetic Operator Squad!
+
Addition
Adds numbers together
5 + 3 = 8
-
Subtraction
Takes numbers away
10 - 4 = 6
*
Multiplication
Multiplies numbers
6 * 7 = 42
/
Division
Divides numbers
15 / 3 = 5
%
Modulus
Remainder after division
17 % 5 = 2
()
Parentheses
Controls order
(5+3)*2 = 16
๐ป Basic Arithmetic in C
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d + %d = %d\n", a, b, a + b);
printf("Subtraction: %d - %d = %d\n", a, b, a - b);
printf("Multiplication: %d * %d = %d\n", a, b, a * b);
printf("Division: %d / %d = %d\n", a, b, a / b);
printf("Modulus: %d %% %d = %d\n", a, b, a % b);
return 0;
}
๐บ Output Preview:
Addition: 10 + 3 = 13
Subtraction: 10 - 3 = 7
Multiplication: 10 * 3 = 30
Division: 10 / 3 = 3
Modulus: 10 % 3 = 1
๐ The Mysterious % (Modulus) Operator
โ
What does % actually do?
17 รท 5 = 3 remainder 2
17 % 5 = 2 (just the remainder!)
20 % 6 = 2 (20 รท 6 = 3 remainder 2)
15 % 4 = 3 (15 รท 4 = 3 remainder 3)
๐ฏ Cool Uses for Modulus:
- ๐ Check if a number is even or odd (n % 2)
- ๐
Find day of week from day number
- ๐ Create repeating patterns in games
- โฐ Convert seconds to minutes and seconds
๐ Order of Operations in Programming
P E M D A S
P - Parentheses
( ) comes first
(5 + 3) * 2 = 16
E - Exponents
Powers (we'll learn later)
pow(2, 3) = 8
M - Multiplication
* comes next
5 + 3 * 2 = 11
D - Division
/ same level as *
10 / 2 * 3 = 15
A - Addition
+ comes after * and /
2 + 3 * 4 = 14
S - Subtraction
- same level as +
10 - 2 + 3 = 11
๐ง Order of Operations Challenge!
๐ฏ What will this calculate?
5 + 3 * 2 - 1
Think through PEMDAS step by step!
๐พ Storing Calculation Results
#include <stdio.h>
int main() {
int length = 10;
int width = 5;
int area;
area = length * width;
printf("Rectangle area: %d square units\n", area);
int double_area = area * 2;
printf("Double the area: %d square units\n", double_area);
return 0;
}
โ
Why Store Results?
- ๐ Use the same calculation multiple times
- ๐ Makes code easier to read and understand
- โก Computer calculates once, uses many times
- ๐ ๏ธ Easier to debug and modify later
๐ญ Math with Different Data Types
int whole_number = 10;
float decimal_number = 3.5;
float result;
int int_division = 10 / 3;
float float_division = 10.0 / 3.0;
result = whole_number + decimal_number;
printf("Integer division: %d\n", int_division);
printf("Float division: %.2f\n", float_division);
printf("Mixed calculation: %.1f\n", result);
โ ๏ธ Integer Division Gotcha!
10 / 3 = 3 (not 3.33) when both numbers are integers!
Solution: Use 10.0 / 3.0 for decimal results
๐ Real-World Math Problems
๐ Room Area Calculator
Calculate the area of your bedroom
area = length * width
printf("Your room is %d sq ft", area);
๐ Pizza Price Per Slice
Find cost per slice for fair sharing
cost_per_slice = total_cost / num_slices
printf("Each slice costs $%.2f", cost_per_slice);
โฐ Time Converter
Convert minutes to hours and minutes
hours = total_minutes / 60
remaining_min = total_minutes % 60
๐ฐ Allowance Savings
Calculate total savings over time
total_saved = weekly_allowance * num_weeks
printf("You'll save $%d", total_saved);
๐งฎ Build Your Super Calculator!
๐ก Coding Challenge: Advanced Calculator
Let's build a calculator that does it all!
1. Ask user for two numbers
2. Display a menu of operations (+, -, *, /, %)
3. Get user's choice
4. Perform the calculation
5. Display the result with formatting
6. Add error messages for division by zero
๐ฏ Example Flow:
Calculator: Enter first number: 15
Calculator: Enter second number: 4
Calculator: Choose operation (+, -, *, /, %): %
Calculator: 15 % 4 = 3
๐จ Common Math Programming Mistakes
โ Integer Division Surprise
int result = 5 / 2; // Result: 2, not 2.5!
Fix: Use float: float result = 5.0 / 2.0;
โ Forgetting Order of Operations
int total = 5 + 3 * 2; // 11, not 16!
Fix: Use parentheses: (5 + 3) * 2
โ Division by Zero
int result = 10 / 0; // Program crash!
Fix: Always check: if (divisor != 0)
โ
Pro Tips!
- Use parentheses to make intentions clear
- Add .0 to numbers for float math
- Always validate user input
- Use meaningful variable names
๐ฌ Preview: Advanced Math Functions
Coming Soon: Math Library Functions
C has built-in functions for complex math operations!
sqrt()
Square root
sqrt(16) = 4
pow()
Power/exponent
pow(2, 3) = 8
abs()
Absolute value
abs(-5) = 5
sin()
Sine function
sin(90ยฐ) = 1
cos()
Cosine function
cos(0ยฐ) = 1
ceil()
Round up
ceil(3.2) = 4
๐ These will let us solve any math problem imaginable!
๐ Mathematical Mastery Achieved!
โ Arithmetic Operator Master
๐ PEMDAS Expert
๐งฎ Calculator Builder
๐พ Result Storage Pro
๐ข Data Type Handler
๐ Real-World Problem Solver
Your computer is now a mathematical powerhouse! ๐
You can now solve any arithmetic problem with code!
๐ Your Mathematical Programming Mission!
๐ This Week's Challenges:
- ๐งฎ Complete the Super Calculator - Include all 5 operators with menu system
- ๐ Room Area & Perimeter Calculator - Ask for length/width, calculate both area and perimeter
- โฐ Time Converter Program - Convert total seconds to hours, minutes, and seconds
- ๐ฐ Shopping Calculator - Calculate total cost, tax, and change from payment
- ๐ฏ PEMDAS Practice - Write 5 complex expressions and predict their results
- ๐ Modulus Explorer - Create a program that shows even/odd checking
๐ Next Week: Session 7
Decision Making - Basic Conditionals
We'll teach computers to make choices and decisions!
๐ค Think About This Week:
What decisions would you like your program to make automatically?
๐ Fantastic work! You're becoming a math programming wizard! ๐