๐Ÿ  Session 6 Homework

Arithmetic Operations & Math Magic

๐Ÿ“š What You'll Practice

This homework focuses on comprehensive arithmetic operations, PEMDAS order of operations, and building a super calculator. You'll master all arithmetic operators and work with different data types.

Goal: Become an expert in arithmetic operations and build powerful calculators!

๐Ÿ“‹ Instructions

๐Ÿ“ Program 1: PEMDAS Practice

Master the order of operations!

What it does:

Demonstrates PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction) with various complex expressions.

#include <stdio.h> int main() { int a, b, c, d; int result1, result2, result3, result4, result5; printf("Enter four numbers: "); scanf("%d %d %d %d", &a, &b, &c, &d); printf("\n=== PEMDAS PRACTICE ===\n"); printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d); // Different PEMDAS expressions result1 = a + b * c; // Multiplication first result2 = (a + b) * c; // Parentheses first result3 = a * b + c * d; // Both multiplications, then addition result4 = (a + b) * (c - d); // Both parentheses, then multiplication result5 = a + b * c - d; // Multiplication, then left-to-right printf("\nExpression Results:\n"); printf("a + b * c = %d\n", result1); printf("(a + b) * c = %d\n", result2); printf("a * b + c * d = %d\n", result3); printf("(a + b) * (c - d) = %d\n", result4); printf("a + b * c - d = %d\n", result5); // Show step-by-step for one complex expression printf("\n=== STEP-BY-STEP: (a + b) * (c - d) ===\n"); printf("Step 1: (a + b) = (%d + %d) = %d\n", a, b, a + b); printf("Step 2: (c - d) = (%d - %d) = %d\n", c, d, c - d); printf("Step 3: %d * %d = %d\n", a + b, c - d, (a + b) * (c - d)); return 0; }

๐Ÿงช Test Cases

  • Input: 2, 3, 4, 1 โ†’ Various PEMDAS calculations
  • Input: 5, 2, 6, 3 โ†’ Various PEMDAS calculations

๐Ÿ”ข Program 2: Mixed Data Type Calculator

Work with int and float in calculations!

What it does:

Demonstrates how different data types behave in calculations and shows the importance of choosing the right data type.

#include <stdio.h> int main() { int int1, int2; float float1, float2; float result1, result2, result3, result4; printf("Enter two integers: "); scanf("%d %d", &int1, &int2); printf("Enter two floats: "); scanf("%f %f", &float1, &float2); printf("\n=== MIXED DATA TYPE CALCULATIONS ===\n"); printf("Integers: %d, %d\n", int1, int2); printf("Floats: %.2f, %.2f\n", float1, float2); // Different type combinations result1 = int1 / int2; // int / int = int (truncated) result2 = float1 / float2; // float / float = float result3 = int1 / float2; // int / float = float result4 = float1 / int2; // float / int = float printf("\nDivision Results:\n"); printf("int / int: %d / %d = %.2f (stored as float)\n", int1, int2, result1); printf("float / float: %.2f / %.2f = %.2f\n", float1, float2, result2); printf("int / float: %d / %.2f = %.2f\n", int1, float2, result3); printf("float / int: %.2f / %d = %.2f\n", float1, int2, result4); // Modulo operations printf("\nModulo Operations:\n"); printf("%d %% %d = %d\n", int1, int2, int1 % int2); printf("Note: Modulo only works with integers!\n"); return 0; }

๐Ÿงช Test Cases

  • Integers: 15, 4 โ†’ 15/4 = 3.00 (truncated), 15%4 = 3
  • Floats: 15.0, 4.0 โ†’ 15.0/4.0 = 3.75 (exact)
  • Mixed: 15/4.0 = 3.75, 15.0/4 = 3.75

๐Ÿ”ข Program 3: Advanced Modulo Operations

Master the modulo operator for practical applications!

What it does:

Demonstrates advanced uses of the modulo operator including checking divisibility, finding patterns, and practical applications.

#include <stdio.h> int main() { int number, divisor; int remainder, quotient; printf("Enter a number: "); scanf("%d", &number); printf("Enter a divisor: "); scanf("%d", &divisor); remainder = number % divisor; quotient = number / divisor; printf("\n=== MODULO ANALYSIS ===\n"); printf("Number: %d\n", number); printf("Divisor: %d\n", divisor); printf("Quotient: %d\n", quotient); printf("Remainder: %d\n", remainder); printf("Verification: %d = %d * %d + %d\n", number, divisor, quotient, remainder); printf("\n=== DIVISIBILITY CHECKS ===\n"); if (remainder == 0) { printf("%d is divisible by %d\n", number, divisor); } else { printf("%d is NOT divisible by %d\n", number, divisor); } // Check for even/odd if (number % 2 == 0) { printf("%d is even\n", number); } else { printf("%d is odd\n", number); } // Check for multiples of 5 if (number % 5 == 0) { printf("%d is a multiple of 5\n", number); } else { printf("%d is NOT a multiple of 5\n", number); } return 0; }

๐Ÿงช Test Cases

  • Number: 17, Divisor: 5 โ†’ 17 = 5 * 3 + 2, not divisible
  • Number: 20, Divisor: 4 โ†’ 20 = 4 * 5 + 0, divisible
  • Number: 25 โ†’ Even/odd check, multiple of 5 check

๐Ÿงฎ Program 4: All Operations Calculator

Perform all arithmetic operations in one program!

What it does:

Gets two numbers from the user and performs all arithmetic operations, showing the results for each operation.

#include <stdio.h> int main() { float num1, num2; float add_result, sub_result, mul_result, div_result; int int1, int2, mod_result; printf("=== ALL OPERATIONS CALCULATOR ===\n"); printf("Enter first number: "); scanf("%f", &num1); printf("Enter second number: "); scanf("%f", &num2); // Perform all operations add_result = num1 + num2; sub_result = num1 - num2; mul_result = num1 * num2; div_result = num1 / num2; // Convert to integers for modulo int1 = (int)num1; int2 = (int)num2; mod_result = int1 % int2; printf("\n=== CALCULATION RESULTS ===\n"); printf("Numbers: %.2f and %.2f\n", num1, num2); printf("Addition: %.2f + %.2f = %.2f\n", num1, num2, add_result); printf("Subtraction: %.2f - %.2f = %.2f\n", num1, num2, sub_result); printf("Multiplication: %.2f * %.2f = %.2f\n", num1, num2, mul_result); printf("Division: %.2f / %.2f = %.2f\n", num1, num2, div_result); printf("Modulo: %d %% %d = %d\n", int1, int2, mod_result); // Show some additional calculations printf("\n=== ADDITIONAL CALCULATIONS ===\n"); printf("Average: (%.2f + %.2f) / 2 = %.2f\n", num1, num2, (num1 + num2) / 2); printf("Sum of squares: %.2fยฒ + %.2fยฒ = %.2f\n", num1, num2, (num1 * num1) + (num2 * num2)); return 0; }

๐Ÿงช Test Cases

  • Input: 15.5, 4.2 โ†’ All operations with 15.5 and 4.2
  • Input: 20, 4 โ†’ All operations with 20 and 4
  • Input: 12, 3 โ†’ All operations with 12 and 3

๐Ÿ“Š Program 5: Mathematical Expression Evaluator

Evaluate complex mathematical expressions!

What it does:

Evaluates complex mathematical expressions step by step, demonstrating PEMDAS and showing intermediate results.

#include <stdio.h> int main() { float a, b, c, d; float step1, step2, step3, final_result; printf("=== MATHEMATICAL EXPRESSION EVALUATOR ===\n"); printf("Enter four numbers: "); scanf("%f %f %f %f", &a, &b, &c, &d); printf("\nExpression: (a + b) * (c - d) + a * b\n"); printf("Values: a=%.2f, b=%.2f, c=%.2f, d=%.2f\n", a, b, c, d); printf("\n=== STEP-BY-STEP EVALUATION ===\n"); // Step 1: Evaluate (a + b) step1 = a + b; printf("Step 1: (a + b) = (%.2f + %.2f) = %.2f\n", a, b, step1); // Step 2: Evaluate (c - d) step2 = c - d; printf("Step 2: (c - d) = (%.2f - %.2f) = %.2f\n", c, d, step2); // Step 3: Evaluate a * b step3 = a * b; printf("Step 3: a * b = %.2f * %.2f = %.2f\n", a, b, step3); // Step 4: Evaluate (a + b) * (c - d) float parentheses_result = step1 * step2; printf("Step 4: (a + b) * (c - d) = %.2f * %.2f = %.2f\n", step1, step2, parentheses_result); // Final result: (a + b) * (c - d) + a * b final_result = parentheses_result + step3; printf("Step 5: %.2f + %.2f = %.2f\n", parentheses_result, step3, final_result); printf("\n=== FINAL RESULT ===\n"); printf("(%.2f + %.2f) * (%.2f - %.2f) + %.2f * %.2f = %.2f\n", a, b, c, d, a, b, final_result); return 0; }

๐Ÿงช Test Cases

  • Input: 2, 3, 5, 1 โ†’ (2+3)*(5-1)+2*3 = 5*4+6 = 26
  • Input: 4, 2, 8, 3 โ†’ (4+2)*(8-3)+4*2 = 6*5+8 = 38