Simvik

Last digit

Use the remainder operator. An easy embedded C problem (Variables and operators), run and graded in the browser.

easy · Variables and operators · remainder

The % operator gives the remainder of a division. 17 % 5 is 2, because 17 is three fives with 2 left over.

Return the last decimal digit of n. n is never negative.

Starter code

int last_digit(int n) {
  return 0;
}

Example tests

A four-digit number

printf("%d\n", last_digit(1234));

Expected output:

4

A single digit

printf("%d\n", last_digit(7));

Expected output:

7

Learn it first

More Variables and operators problems