#include <stdio.h>
int main(void) {
int a, b, c, d, e, min, max, n;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
min = a;
if (a <= b && a <= c && a <= d && a <= e)
min = a;
else if (b <= a && b <= c && b <= d && b <= e)
min = b;
else if (c <= b && c <= a && c <= d && c <= e)
min = c;
else if (d <= b && d <= c && d <= a && d <= e)
min = d;
else if (e <= b && e <= c && e <= d && e <= a)
min = e;
max = a;
if (max < b) max = b;
if (max < c) max = c;
if (max < d) max = d;
if (max < e) max = e;
n = max +min;
printf("%d", n);
return 0;
}
This program reads five integers, finds the smallest and largest among them, and prints their sum.
- The code for finding the minimum is overly complex and has a logical flaw. The first condition checks if `a` is less than or equal to all others, but the `else if` structure means only one branch can execute. If `a` is not the minimum, the code checks `b`, but the condition for `b` compares it to `a` and others — however, if `a` was not the minimum, `b` might still not be the minimum, and the logic can fail when two numbers are equal or when the minimum appears later. Consider a simpler approach: start with `min = a`, then compare and update `min` with each subsequent number using a single `if` statement per number, similar to how you handled `max`.
- The `max` calculation is correct and straightforward. The `min` calculation should follow the same pattern: initialize `min = a`, then for each of `b, c, d, e`, if that number is less than `min`, update `min`. This avoids the complex chained conditions and the `else if` problem.