basics · beginner · ~15 min

Max of three

Use `if/else` to compare values.

Challenge

Return the largest of three integers.

Task

Implement int max3(int a, int b, int c) that returns the greatest of the three values. No main — the grader calls it.

Input

Three int arguments. Values may repeat and may be negative.

Output

The single largest int. If two or three are tied for the largest, return that value.

Example

max3(1, 2, 3)     ->   3
max3(5, 1, 4)     ->   5
max3(-1, -2, -3)  ->   -1
max3(2, 2, 2)     ->   2

Edge cases

  • All equal: return that value.
  • All negative: the one closest to zero is the largest.

Input format

Three int arguments a, b, c.

Output format

The largest of the three ints.

Constraints

a, b, and c are 32-bit signed integers. No dynamic allocation.

Starter code

int max3(int a, int b, int c) {
    /* TODO */
    return 0;
}

Common mistakes

Using nested if-else branches that miss tie conditions where two or more arguments share the maximum value.

Edge cases to handle

All three values equal, two values equal and strictly greater than the third, negative values, INT_MIN.

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.