A palindrome number reads the same both ways, like 12321, 1221, 7. Write is_palindrome(n): return true if it is one, false if not. Negative numbers don't count (-121 backwards is 121-). Idea: flip n and see if it matches the original.
is_palindrome(12321)→Trueis_palindrome(123)→Falseis_palindrome(7)→Truebool is_palindrome(int n)#include <stdbool.h>
bool is_palindrome(int n) {
if (n < 0) {
return false;
}
int x = n;
int r = 0;
while (x > 0) {
// your code here: add the last digit of x to the end of r
x = x / 10;
}
// your code here: if the flipped r equals the original n, it is a palindrome (above return false)
return false;
}
Run tries the examples. When they look right, Submit.