basics · beginner · ~15 min

Negate a value

Provide a definition for a declared function.

Challenge

Implement a declared function that returns the negation of its argument.

Task

Implement int negate(int n) that returns -n. The linker matches this definition to its header declaration at build time. No main — the grader calls it.

Input

A single int n.

Output

Returns -n, as an int.

Example

negate(5)    ->   -5
negate(-7)   ->   7
negate(0)    ->   0

Edge cases

  • Negating 0 gives 0.

Input format

A single int n.

Output format

-n, as an int.

Starter code

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

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