Kodewick
Start learning →
Starter←→0 / 50

Palindrome number

#020Starterr == n6 min

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.

Examples
is_palindrome(12321)→True
is_palindrome(123)→False
is_palindrome(7)→True
The functionbool is_palindrome(int n)
main.c
#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;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.