basics · beginner · ~15 min

Square a number

Use an arithmetic expression in a return.

Challenge

Return the square of an integer.

Task

Implement int square(int n) that returns n * n. No main — the grader calls it.

Input

A single int n.

Output

Returns n squared, as an int.

Example

square(4)    ->   16
square(-3)   ->   9
square(0)    ->   0

Edge cases

  • A negative input still produces a non-negative square.

Input format

A single int n.

Output format

n * n, as an int.

Starter code

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

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