๐Ÿ  Session 4 Homework

Arithmetic Operations

๐Ÿ“š What You'll Practice

This homework focuses on mastering arithmetic operations in C programming. You'll learn about operators, order of operations, and how to perform calculations.

Goal: Become comfortable with mathematical operations and PEMDAS order of operations!

๐Ÿ“‹ Instructions

๐Ÿงฎ Program 1: Basic Calculator

Perform all basic arithmetic operations!

What it does:

Gets two numbers from the user and performs all basic arithmetic operations (addition, subtraction, multiplication, division, modulo).

#include <stdio.h> int main() { int num1, num2; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); printf("\n=== CALCULATION RESULTS ===\n"); printf("%d + %d = %d\n", num1, num2, num1 + num2); printf("%d - %d = %d\n", num1, num2, num1 - num2); printf("%d * %d = %d\n", num1, num2, num1 * num2); printf("%d / %d = %d\n", num1, num2, num1 / num2); printf("%d %% %d = %d\n", num1, num2, num1 % num2); return 0; }

๐Ÿงช Test Cases

  • Input: 15, 4 โ†’ 15+4=19, 15-4=11, 15*4=60, 15/4=3, 15%4=3
  • Input: 20, 6 โ†’ 20+6=26, 20-6=14, 20*6=120, 20/6=3, 20%6=2

๐Ÿ“ Program 2: PEMDAS Practice

Demonstrate order of operations!

What it does:

Shows how parentheses affect the order of operations and demonstrates PEMDAS (Parentheses, Exponents, Multiplication, Division, Addition, Subtraction).

#include <stdio.h> int main() { int a = 10, b = 5, c = 2; int result1, result2, result3, result4; // Different ways to calculate: a + b * c result1 = a + b * c; // Multiplication first: 10 + (5 * 2) = 20 result2 = (a + b) * c; // Parentheses first: (10 + 5) * 2 = 30 result3 = a * b + c; // Multiplication first: (10 * 5) + 2 = 52 result4 = a * (b + c); // Parentheses first: 10 * (5 + 2) = 70 printf("a = %d, b = %d, c = %d\n", a, b, c); printf("\n=== ORDER OF OPERATIONS ===\n"); printf("a + b * c = %d (multiplication first)\n", result1); printf("(a + b) * c = %d (parentheses first)\n", result2); printf("a * b + c = %d (multiplication first)\n", result3); printf("a * (b + c) = %d (parentheses first)\n", result4); return 0; }

๐Ÿงช Expected Output

  • a = 10, b = 5, c = 2
  • a + b * c = 20 (10 + 10)
  • (a + b) * c = 30 (15 * 2)
  • a * b + c = 52 (50 + 2)
  • a * (b + c) = 70 (10 * 7)

๐Ÿ”ข Program 3: Float Division

Work with decimal results!

What it does:

Demonstrates the difference between integer and float division, showing how to get decimal results.

#include <stdio.h> int main() { int a = 15, b = 4; float x = 15.0, y = 4.0; float result1, result2; printf("=== INTEGER vs FLOAT DIVISION ===\n"); printf("a = %d, b = %d\n", a, b); printf("x = %.1f, y = %.1f\n", x, y); printf("\nInteger division: %d / %d = %d\n", a, b, a / b); printf("Float division: %.1f / %.1f = %.2f\n", x, y, x / y); // Mixed operations result1 = a / y; // int / float = float result2 = x / b; // float / int = float printf("\nMixed operations:\n"); printf("%d / %.1f = %.2f\n", a, y, result1); printf("%.1f / %d = %.2f\n", x, b, result2); return 0; }

๐Ÿงช Expected Output

  • Integer division: 15 / 4 = 3 (truncated)
  • Float division: 15.0 / 4.0 = 3.75 (exact)
  • Mixed: 15 / 4.0 = 3.75, 15.0 / 4 = 3.75

๐Ÿ”ข Program 4: Modulo Operations

Master the modulo operator!

What it does:

Demonstrates various uses of the modulo operator (%) for finding remainders and checking divisibility.

#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("\n=== MODULO OPERATIONS ===\n"); printf("Number: %d\n", num); printf("%d %% 2 = %d (remainder when divided by 2)\n", num, num % 2); printf("%d %% 3 = %d (remainder when divided by 3)\n", num, num % 3); printf("%d %% 5 = %d (remainder when divided by 5)\n", num, num % 5); printf("%d %% 10 = %d (remainder when divided by 10)\n", num, num % 10); printf("\n=== DIVISIBILITY CHECKS ===\n"); if (num % 2 == 0) { printf("%d is divisible by 2 (even number)\n", num); } else { printf("%d is not divisible by 2 (odd number)\n", num); } if (num % 3 == 0) { printf("%d is divisible by 3\n", num); } else { printf("%d is not divisible by 3\n", num); } return 0; }

๐Ÿงช Test Cases

  • Input: 12 โ†’ 12%2=0 (even), 12%3=0 (divisible by 3), 12%5=2, 12%10=2
  • Input: 17 โ†’ 17%2=1 (odd), 17%3=2 (not divisible by 3), 17%5=2, 17%10=7

๐Ÿงฎ Program 5: Complex Expression Calculator

Calculate complex mathematical expressions!

What it does:

Calculates complex mathematical expressions using multiple operations and demonstrates the importance of parentheses.

#include <stdio.h> int main() { int a, b, c; int result1, result2, result3, result4; printf("Enter three numbers: "); scanf("%d %d %d", &a, &b, &c); printf("\n=== COMPLEX EXPRESSIONS ===\n"); printf("a = %d, b = %d, c = %d\n", a, b, c); // Different complex expressions result1 = a + b * c; // a + (b * c) result2 = (a + b) * c; // (a + b) * c result3 = a * b + c * a; // (a * b) + (c * a) result4 = (a + b) * (c - a); // (a + b) * (c - a) printf("\nExpression Results:\n"); printf("a + b * c = %d\n", result1); printf("(a + b) * c = %d\n", result2); printf("a * b + c * a = %d\n", result3); printf("(a + b) * (c - a) = %d\n", result4); // Show step-by-step calculation for one expression printf("\n=== STEP-BY-STEP: (a + b) * (c - a) ===\n"); printf("Step 1: (a + b) = (%d + %d) = %d\n", a, b, a + b); printf("Step 2: (c - a) = (%d - %d) = %d\n", c, a, c - a); printf("Step 3: %d * %d = %d\n", a + b, c - a, (a + b) * (c - a)); return 0; }

๐Ÿงช Test Cases

  • Input: 5, 3, 8 โ†’ Various complex calculations
  • Input: 10, 4, 6 โ†’ Various complex calculations