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

A) 15

(5 + 3) * 2 - 1

B) 9

5 + (3 * 2 - 1)

C) 10

5 + (3 * 2) - 1

D) 14

(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; // Calculate and store the result area = length * width; printf("Rectangle area: %d square units\n", area); // We can use the stored result in more calculations 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; // Integer math - loses decimal part int int_division = 10 / 3; // Result: 3 (not 3.33...) // Float math - keeps decimal precision float float_division = 10.0 / 3.0; // Result: 3.333333 // Mixing types - result becomes float result = whole_number + decimal_number; // 10 + 3.5 = 13.5 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! ๐ŸŽŠ