🏠 Session 5 Homework

Math Magic & Calculations

📚 What You'll Practice

This homework focuses on advanced mathematical calculations and building practical calculators. You'll work with complex expressions and real-world math problems.

Goal: Master advanced calculations and build useful mathematical tools!

📋 Instructions

📐 Program 1: Area Calculator

Calculate areas of different shapes!

What it does:

Calculates the area of a rectangle, circle, and triangle using user input dimensions.

#include <stdio.h> int main() { float length, width, radius, base, height; float rect_area, circle_area, triangle_area; float pi = 3.14159; printf("=== AREA CALCULATOR ===\n"); // Rectangle area printf("Enter rectangle length: "); scanf("%f", &length); printf("Enter rectangle width: "); scanf("%f", &width); rect_area = length * width; // Circle area printf("Enter circle radius: "); scanf("%f", &radius); circle_area = pi * radius * radius; // Triangle area printf("Enter triangle base: "); scanf("%f", &base); printf("Enter triangle height: "); scanf("%f", &height); triangle_area = 0.5 * base * height; printf("\n=== CALCULATED AREAS ===\n"); printf("Rectangle area: %.2f square units\n", rect_area); printf("Circle area: %.2f square units\n", circle_area); printf("Triangle area: %.2f square units\n", triangle_area); return 0; }

🧪 Test Cases

  • Rectangle: 5x3 → Area = 15.00
  • Circle: radius 4 → Area = 50.27
  • Triangle: base 6, height 8 → Area = 24.00

🌡️ Program 2: Temperature Converter

Convert between Celsius and Fahrenheit!

What it does:

Converts temperature between Celsius and Fahrenheit using the standard conversion formulas.

#include <stdio.h> int main() { float celsius, fahrenheit; int choice; printf("=== TEMPERATURE CONVERTER ===\n"); printf("1. Celsius to Fahrenheit\n"); printf("2. Fahrenheit to Celsius\n"); printf("Enter your choice (1 or 2): "); scanf("%d", &choice); if (choice == 1) { printf("Enter temperature in Celsius: "); scanf("%f", &celsius); fahrenheit = (celsius * 9.0 / 5.0) + 32; printf("%.2f°C = %.2f°F\n", celsius, fahrenheit); } else if (choice == 2) { printf("Enter temperature in Fahrenheit: "); scanf("%f", &fahrenheit); celsius = (fahrenheit - 32) * 5.0 / 9.0; printf("%.2f°F = %.2f°C\n", fahrenheit, celsius); } else { printf("Invalid choice!\n"); } return 0; }

🧪 Test Cases

  • Choice 1, 25°C → 77.00°F
  • Choice 2, 86°F → 30.00°C
  • Choice 1, 0°C → 32.00°F

💰 Program 3: Compound Interest Calculator

Calculate compound interest for investments!

What it does:

Calculates compound interest using the formula: A = P(1 + r/n)^(nt), where P is principal, r is rate, n is compounding frequency, and t is time.

#include <stdio.h> #include <math.h> int main() { float principal, rate, time, amount, interest; int n; // compounding frequency per year printf("=== COMPOUND INTEREST CALCULATOR ===\n"); printf("Enter principal amount: $"); scanf("%f", &principal); printf("Enter annual interest rate (as decimal): "); scanf("%f", &rate); printf("Enter time in years: "); scanf("%f", &time); printf("Enter compounding frequency per year: "); scanf("%d", &n); // Calculate compound interest amount = principal * pow((1 + rate / n), n * time); interest = amount - principal; printf("\n=== CALCULATION RESULTS ===\n"); printf("Principal: $%.2f\n", principal); printf("Interest Rate: %.2f%%\n", rate * 100); printf("Time: %.1f years\n", time); printf("Compounding: %d times per year\n", n); printf("Final Amount: $%.2f\n", amount); printf("Interest Earned: $%.2f\n", interest); return 0; }

🧪 Test Cases

  • Principal: $1000, Rate: 0.05, Time: 2 years, Frequency: 12 → Final: $1104.94
  • Principal: $5000, Rate: 0.08, Time: 5 years, Frequency: 4 → Final: $7429.74

⚖️ Program 4: BMI Calculator

Calculate Body Mass Index!

What it does:

Calculates BMI using the formula: BMI = weight(kg) / height(m)² and categorizes the result.

#include <stdio.h> int main() { float weight, height, bmi; char name[50]; printf("=== BMI CALCULATOR ===\n"); printf("Enter your name: "); scanf("%s", name); printf("Enter your weight in kg: "); scanf("%f", &weight); printf("Enter your height in meters: "); scanf("%f", &height); // Calculate BMI bmi = weight / (height * height); printf("\n=== BMI RESULTS ===\n"); printf("Name: %s\n", name); printf("Weight: %.1f kg\n", weight); printf("Height: %.2f m\n", height); printf("BMI: %.2f\n", bmi); // BMI Categories printf("\n=== BMI CATEGORY ===\n"); if (bmi < 18.5) { printf("Underweight (BMI < 18.5)\n"); } else if (bmi >= 18.5 && bmi < 25) { printf("Normal weight (BMI 18.5-24.9)\n"); } else if (bmi >= 25 && bmi < 30) { printf("Overweight (BMI 25-29.9)\n"); } else { printf("Obese (BMI >= 30)\n"); } return 0; }

🧪 Test Cases

  • Weight: 70kg, Height: 1.75m → BMI: 22.86 (Normal weight)
  • Weight: 60kg, Height: 1.60m → BMI: 23.44 (Normal weight)
  • Weight: 90kg, Height: 1.80m → BMI: 27.78 (Overweight)

🔬 Program 5: Mathematical Constants and Functions

Work with mathematical constants and basic functions!

What it does:

Demonstrates mathematical constants and basic mathematical operations using predefined values.

#include <stdio.h> #include <math.h> int main() { float radius = 5.0; float pi = 3.14159; float area, circumference; float base = 4.0; float height = 3.0; float triangle_area; printf("=== MATHEMATICAL CALCULATIONS ===\n"); // Circle calculations area = pi * radius * radius; circumference = 2 * pi * radius; printf("Circle with radius %.1f:\n", radius); printf("Area: %.2f square units\n", area); printf("Circumference: %.2f units\n", circumference); // Triangle calculations triangle_area = 0.5 * base * height; printf("\nTriangle with base %.1f and height %.1f:\n", base, height); printf("Area: %.2f square units\n", triangle_area); // Square calculations float side = 6.0; float square_area = side * side; float square_perimeter = 4 * side; printf("\nSquare with side %.1f:\n", side); printf("Area: %.2f square units\n", square_area); printf("Perimeter: %.2f units\n", square_perimeter); return 0; }

🧪 Expected Output

  • Circle: radius 5.0 → Area: 78.54, Circumference: 31.42
  • Triangle: base 4.0, height 3.0 → Area: 6.00
  • Square: side 6.0 → Area: 36.00, Perimeter: 24.00