basics · beginner · ~15 min

Smaller of two

Mirror the max logic for the minimum.

Challenge

Return the smaller of two integers.

Task

Implement int min2(int a, int b) that returns the smaller of the two values (return either when they are equal). No main — the grader calls it.

Input

Two ints a and b.

Output

Returns the smaller value, as an int.

Example

min2(3, 7)     ->   3
min2(9, 2)     ->   2
min2(-1, -4)   ->   -4

Edge cases

  • Equal inputs return that shared value.
  • Negative values compare normally.

Input format

Two ints a and b.

Output format

The smaller of a and b, as an int.

Constraints

a and b are 32-bit signed integers.

Starter code

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

Common mistakes

Confusing the less-than and greater-than relational operators.

Edge cases to handle

a == b, negative numbers, INT_MIN/INT_MAX.

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