Reverse Number in C

Level 3101 keystrokes

Reverses the digits of an integer using modulo and division.

C code

// Reverse 1234; rev builds the reversed number, starting at 0
1int num = 1234, rev = 0;
// One pass per digit of num
2while (num > 0) {
// num % 10 is the last digit; rev * 10 makes room for it: 4, 43, 432, 4321
3 rev = rev * 10 + num % 10;
// Drop the digit that was just used
4 num /= 10;
5}
// Print 4321
6printf("%d", rev);

Output

4321
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.

  • ;× 4
  • =× 4
  • ,× 2
  • "× 2
  • (× 2
  • )× 2
  • %× 2
  • {× 1
  • }× 1
  • *× 1

More C examples at level 3

← C typing practice