Simvik

Count the digits

Loop until a value runs out. An easy embedded C problem (Decisions and loops), run and graded in the browser.

easy · Decisions and loops · loops · while

Return how many decimal digits n has. n is never negative, and 0 has one digit.

Dividing by 10 removes the last digit, so count how many times you can do that.

Starter code

int count_digits(int n) {
  return 0;
}

Example tests

One digit

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

Expected output:

1

Four digits

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

Expected output:

4

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