basics · beginner · ~15 min
Use an arithmetic expression in a return.
Return the square of an integer.
Implement int square(int n) that returns n * n. No main — the grader calls it.
A single int n.
Returns n squared, as an int.
square(4) -> 16
square(-3) -> 9
square(0) -> 0
A single int n.
n * n, as an int.
n is an integer such that n * n does not overflow signed 32-bit int in tests.
int square(int n) {
/* TODO */
return 0;
}
Using the caret '^' operator, which in C is bitwise XOR rather than exponentiation.
n = 0 (result 0), n = 1 (result 1), negative values (e.g. -5 -> 25).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.