Is Palindrome in C

Level 3135 keystrokes

Checks if a number is a palindrome by reversing its digits.

C code

// orig keeps a copy, because the loop destroys num
1int num = 121, orig = num, rev = 0;
// Build the reversed number one digit at a time
2while (num > 0) {
// Shift rev left by one digit and add the last digit of num: 1, 12, 121
3 rev = rev * 10 + num % 10;
// Remove that digit from num
4 num /= 10;
5}
// The ternary ?: picks "Yes" because 121 reads the same reversed
6printf("%s", orig == rev ? "Yes" : "No");

Output

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

  • =× 7
  • "× 6
  • ;× 4
  • ,× 3
  • (× 2
  • )× 2
  • %× 2
  • :× 1
  • ?× 1
  • {× 1

More C examples at level 3

← C typing practice