Count Digits in C

Level 3100 keystrokes

Counts the number of digits in an integer by repeatedly dividing by 10.

C code

// A 5-digit number
1int num = 12345;
// Number of digits found so far
2int count = 0;
// Repeat until every digit has been removed
3while (num > 0) {
// One more digit
4 count++;
// Integer division drops the last digit: 12345 → 1234 → … → 0
5 num /= 10;
6}
// The loop ran 5 times: Digits: 5 (with num = 0 it would print 0)
7printf("Digits: %d", count);

Output

Digits: 5
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 5
  • =× 3
  • "× 2
  • (× 2
  • )× 2
  • +× 2
  • ,× 1
  • :× 1
  • {× 1
  • }× 1

More C examples at level 3

← C typing practice