Search code examples
cfunctionintegerpalindrome

Palindromic number in C


So lets say we have this program in C that checks if a number is palindromic or not, WITHOUT the use of any array at all(so dont respond with any answer that uses an array)

#include <stdio.h>
#include <math.h>

int NumOfDigits(long long x) {
    int sum = 1;
    while (x / 10 != 0) {
        sum ++;
        x = x / 10;
    }
    return sum;
}

int isPal(long long x) {
    int f, c, front, back, sum;
    sum = NumOfDigits(x);
    
    c = round(pow(10,(sum-2)));
    front = x / round(pow(10,(sum - 1)));
    back = x % 10;
    f = 1; 

    while (x != 0 && f == 1 && c != 0) {
        if (front == back) {
           x = (x / 10) % c;
           c /= 100;
           sum -=2;
           front = x / round(pow(10,(sum-1)));
           back = x % 10;
        } else {
           f = 0;
        }
    }
    if (f) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    int f;
    long long x;
    scanf("%lld", &x);
    f = isPal(x);
    if (f) {
        printf("yes");
    } else {
        printf("no");
    }
    printf("\n");
}

So basically this algorithm checks the first and the last digit each time and then reduces the NumOfDigits by 2, so if we have 345543, first it's 345543 then 4554, 55 etc. The point with this program is that, for example given the number 900075181570009 it says its NOT palindromic, but it is because the computer erases the 0s form the left side of the number. So when it goes to 0007518157000 its basically 7518157000 which is not a palindromic number. So, how can we modify the algorithm, again without the use of an array, to achieve the expected result?


Solution

  • In testing out the code I did notice that the result of 900075181570009 being designated as not a palindrome was an issue with the variable being an integer, and thus being too small in some cases to contain the proper power of 10. When I designated that as a "long long" variable, the number 900075181570009 was designated as a palindrome.

    In regard to testing numbers that had leading zeros (and trailing zeros) what appeared to be a necessary function to repeatedly divide the number by 10 until all trailing zeros were expunged, and then perform the palindrome test. With that following is the refactored code. First off is the additional function to remove trailing zeros from a candidate number.

    #include <stdio.h>
    #include <math.h>
    
    long long lagging(long long z)
    {
        long long work = z;
    
        while (1)
        {
            if ((work % 10) != 0)
                break;
    
            work /= 10;
        }
    
        return work;
    }
    

    Next, is the refactored palindrome test function where the variable "c" has been enlarged, along with the initial scrubbing of a test value.

        int isPal(long long x) {
        int f,front, back, sum;
        long long c;
    
        x = lagging(x);  /* One off call to additional scrubbing if needed */
    
        sum = NumOfDigits(x);
    

    In testing out various numbers noted in your example, following was the test output at the terminal.

    craig@Vera:~/C_Programs/Console/PalInteger/bin/Release$ ./PalInteger 
    Enter a number: 900075181570009
    yes
    craig@Vera:~/C_Programs/Console/PalInteger/bin/Release$ ./PalInteger 
    Enter a number: 0007518157000
    yes
    craig@Vera:~/C_Programs/Console/PalInteger/bin/Release$ ./PalInteger 
    Enter a number: 345543
    yes
    craig@Vera:~/C_Programs/Console/PalInteger/bin/Release$ ./PalInteger 
    Enter a number: 7518157000
    yes
    

    One for train of thought for your evaluation.