Task: Read three integers, compute their sum and (floating) average. Also evaluate one complex expression twice—once as-is and once with parentheses—to demonstrate operator precedence.
/*
Program: Sum & Average + Operator Precedence (U1)
What it does:
1) Reads three integers a, b, c.
2) Computes sum = a + b + c and average = sum / 3.0 (note the .0 to avoid integer division).
3) Evaluates a complex expression in two ways:
- Raw : a + b * c - 10 / 2 (multiplication/division happen before addition/subtraction)
- Paren : ((a + b) * c) - (10 / 2) (parentheses change the evaluation order)
*/
#include <stdio.h>
int main(void) {
int a, b, c;
int sum;
double avg;
int expr_raw, expr_paren;
printf("Enter three integers (a b c): ");
if (scanf("%d %d %d", &a, &b, &c) != 3) {
printf("Invalid input. Please enter exactly three integers.\n");
return 0;
}
sum = a + b + c;
avg = sum / 3.0; /* 3.0 ensures floating-point division, not integer division */
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", avg);
/* Operator precedence demo: * and / before + and -, and left-to-right associativity */
expr_raw = a + b * c - 10 / 2;
expr_paren = ((a + b) * c) - (10 / 2);
printf("Expression (raw) : a + b * c - 10 / 2 = %d\n", expr_raw);
printf("Expression (paren) : ((a + b) * c) - (10 / 2) = %d\n", expr_paren);
return 0;
}