Simvik

Sign of a number

Handle three cases with if, else if and else. An easy embedded C problem (Decisions and loops), run and graded in the browser.

easy · Decisions and loops · if · else

Return 1 if n is positive, -1 if it is negative and 0 if it is zero.

Starter code

int sign(int n) {
  return 0;
}

Example tests

A positive number

printf("%d\n", sign(12));

Expected output:

1

A negative number

printf("%d\n", sign(-8));

Expected output:

-1

Learn it first

  • Decisions and loops · Choosing with if and switch, repeating with while and for, and the loop that never ends.

More Decisions and loops problems