Thursday, May 9, 2024
HomeC ProgrammingPalindromic Numbers – Answer | C For Dummies Weblog

Palindromic Numbers – Answer | C For Dummies Weblog


The problem for this month’s Train is to output the primary 100 palindromic numbers, that are integers that mirror the identical digits on both facet. Quite than devise a fancy mathematical equation to find out these values, I cheated.

Sure, similar to the prior month’s Train (Cyclops numbers), I convert the integer worth to a string, then use my helpful strrev() perform to match the unique with the reversed string. After they match, a palindromic worth is discovered.

Right here is the fundamental() perform from my resolution (the itemizing omits the strrev() perform).

2022_10-Train.c


int fundamental()
{
    const int whole = 100;
    const int dimension = 5;        
    const int columns = 9;
    int rely,v,x,y;
    int outcomes[total];
    char fwd[size+1];        
    char *bkd;

    
    rely = 0;
    v = 1;
    whereas(rely<whole)
    {
        
        if( v < 10 )
        {
            outcomes[count] = v;
            rely++;
        }
        else
        {
            snprintf(fwd,dimension,"%d",v);
            bkd = strrev(fwd);
            if(strcmp(bkd,fwd) == 0 )
            {
                outcomes[count] = v;
                rely++;
            }
        }
        v++;
    }

    
    x = 0;
    whereas( rely )
    {
        for( y=0; y<columns; y++ )
        {
            printf("%5d",outcomes[(x*columns)+y]);
            count--;                
            if( !rely )            
                break;
            if( y<columns-1)
                putchar('t');
        }
        x++;
        putchar('n');
    }

    return(0);
}

A whereas loop counts the palindromic numbers, zero by way of 100. An if check handles single digit values (v < 10) as these are all thought-about palindromic.

The else portion of the choice makes use of the snprintf() perform to transform integer v right into a string fwd[]. The strrev() perform reverses this string into a brand new string, char pointer bkd. The strcmp perform compares the 2 values in an if resolution. After they match, the quantity is palindromic. Its worth is saved in array outcomes[] and variable rely is incremented.

The fundamental() perform’s second whereas loop outputs the leads to a fancy desk. The output is proven in Determine 1.

Determine 1. Output from my resolution, utilizing a desk borrowed from final month’s resolution.

I’d be curious to see a mathematical resolution for this train. I assume you might use the modulo operator or maybe some logarithmic witchcraft to peel off digits from the unique integer. However even for those who might re-splice these values into a brand new, reversed integer, how do you check to find out whether or not the 2 values are palindromic? Do the eggheads have an answer?

Regardless, I hope your resolution, nonetheless it’s achieved, met with success.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments