🔧 C Programming Lab: Operators

Experiment 2: Mastering Arithmetic Operations

For BTech Students

📚 Learning Objectives

By the end of this lab, you will be able to:

  • Understand and use arithmetic operators in C programming
  • Implement programs for geometric calculations
  • Apply mathematical formulas in C programs
  • Handle user input and formatted output
  • Debug and test arithmetic operations
🎯 Operators in C Programming

What are Operators?

Operators are symbols that tell the compiler to perform specific mathematical, logical, or relational operations. In C, arithmetic operators are used to perform basic mathematical calculations.

Arithmetic Operators in C:

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 15 / 3 5
% Modulus (remainder) 5 % 3 2
🔬 Experiment 1: Rectangle Area and Perimeter Calculator

Mathematical Formulas:

Area of Rectangle: Area = length × width

Perimeter of Rectangle: Perimeter = 2 × (length + width)

#include <stdio.h> int main() { float length, width, area, perimeter; // Input from user printf("=== Rectangle Calculator ===\n"); printf("Enter length of rectangle: "); scanf("%f", &length); printf("Enter width of rectangle: "); scanf("%f", &width); // Calculate area and perimeter area = length * width; // Multiplication operator perimeter = 2 * (length + width); // Addition and multiplication operators // Display results printf("\n--- Results ---\n"); printf("Length: %.2f units\n", length); printf("Width: %.2f units\n", width); printf("Area: %.2f square units\n", area); printf("Perimeter: %.2f units\n", perimeter); return 0; }
Key Learning Points:
  • Use of multiplication operator (*) for area calculation
  • Combination of addition (+) and multiplication (*) for perimeter
  • Proper use of scanf() for input and printf() for formatted output
  • Float data type for handling decimal values

🧮 Interactive Rectangle Calculator

Enter Dimensions:

Results:

Enter dimensions and click calculate...
🌡️ Experiment 2: Temperature Converter (Celsius to Fahrenheit)

Temperature Conversion Formula:

Fahrenheit = (Celsius × 9/5) + 32

This formula uses multiplication, division, and addition operators in a specific order following operator precedence.

#include <stdio.h> int main() { float celsius, fahrenheit; // Input temperature in Celsius printf("=== Temperature Converter ===\n"); printf("Enter temperature in Celsius: "); scanf("%f", &celsius); // Convert Celsius to Fahrenheit fahrenheit = (celsius * 9.0 / 5.0) + 32.0; // Display the result printf("\n--- Conversion Result ---\n"); printf("Temperature in Celsius: %.2f°C\n", celsius); printf("Temperature in Fahrenheit: %.2f°F\n", fahrenheit); // Additional information if (fahrenheit >= 100.0) { printf("Status: Water boiling point or above!\n"); } else if (fahrenheit <= 32.0) { printf("Status: Water freezing point or below!\n"); } else { printf("Status: Moderate temperature\n"); } return 0; }
Key Learning Points:
  • Operator precedence: multiplication and division before addition
  • Use of parentheses to control order of operations
  • Working with floating-point arithmetic (9.0/5.0 vs 9/5)
  • Conditional statements for additional program logic

🌡️ Interactive Temperature Converter

Enter Temperature:

Results:

Enter temperature and click convert...
🧠 Quick Quiz: Test Your Understanding
1. What will be the result of: 5 * 3 + 2?
A) 25
B) 17
C) 21
D) 13
2. In the temperature formula F = (C * 9/5) + 32, which operations are performed first?
A) Addition
B) Multiplication and Division
C) Only Multiplication
D) Only Division
3. What data type is best for storing decimal measurements like length and width?
A) int
B) float
C) char
D) double only
💡 Programming Tips & Best Practices

Best Practices for Operator Usage:

  • Use parentheses to make operator precedence clear
  • Choose appropriate data types: Use float/double for decimal calculations
  • Avoid integer division: Use 9.0/5.0 instead of 9/5 for precise results
  • Format output: Use %.2f for clean decimal display
  • Validate input: Check for reasonable input values
  • Add comments: Explain complex calculations

Common Mistakes to Avoid:

  • Integer division when you need decimal results
  • Forgetting to handle negative inputs
  • Not using proper format specifiers in printf/scanf
  • Ignoring operator precedence rules
  • Not initializing variables before use
🎯 Lab Assignment & Practice Problems

Practice Problems:

  1. Circle Calculator: Write a program to calculate area and circumference of a circle (π = 3.14159)
  2. Triangle Area: Calculate area using Heron's formula or base×height/2
  3. Temperature Range: Convert Fahrenheit to Celsius (reverse formula)
  4. BMI Calculator: Calculate Body Mass Index = weight/(height²)
  5. Simple Interest: Calculate SI = (Principal × Rate × Time)/100

Assessment Criteria:

  • Correct use of arithmetic operators
  • Proper variable declarations and data types
  • User-friendly input/output format
  • Code commenting and documentation
  • Testing with different input values