basics · beginner · ~15 min

Sign of a number

Branch on a value's sign.

Challenge

Reduce a number to its sign: negative, zero, or positive.

Task

Implement int sign_of(long n) that returns -1 if n is negative, 0 if n is zero, and 1 if n is positive. No main — the grader calls it.

Input

A single long n.

Output

Returns -1, 0, or 1.

Example

sign_of(-9)   ->   -1
sign_of(0)    ->   0
sign_of(42)   ->   1

Edge cases

  • Zero maps to 0 (neither negative nor positive).

Input format

A single long n.

Output format

-1, 0, or 1 for negative, zero, or positive n.

Starter code

int sign_of(long n) {
    /* TODO */
    return 0;
}

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