{"id":5557,"date":"2022-10-08T00:01:01","date_gmt":"2022-10-08T07:01:01","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5557"},"modified":"2022-10-01T10:36:19","modified_gmt":"2022-10-01T17:36:19","slug":"palindromic-numbers-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5557","title":{"rendered":"Palindromic Numbers &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5533\">this month&#8217;s Exercise<\/a> is to output the first 100 palindromic numbers, which are integers that reflect the same digits on either side. Rather than devise a complex mathematical equation to determine these values, I cheated.<br \/>\n<!--more--><br \/>\nYes, just like the prior month&#8217;s Exercise (<a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5499\">Cyclops numbers<\/a>), I convert the integer value to a string, then use my handy <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5541\"><em>strrev()<\/em> function<\/a> to compare the original with the reversed string. When they match, a palindromic value is found.<\/p>\n<p>Here is the <em>main()<\/em> function from my solution (the listing omits the <em>strrev()<\/em> function).<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_10-Exercise.c\" rel=\"noopener\" target=\"_blank\">2022_10-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    const int total = 100;\r\n    const int size = 5;        <span class=\"comments\">\/* assume 5 digits max *\/<\/span>\r\n    const int columns = 9;\r\n    int count,v,x,y;\r\n    int results[total];\r\n    char fwd[size+1];        <span class=\"comments\">\/* +1 for the null char *\/<\/span>\r\n    char *bkd;\r\n\r\n    <span class=\"comments\">\/* find the palindromic values *\/<\/span>\r\n    count = 0;\r\n    v = 1;\r\n    while(count&lt;total)\r\n    {\r\n        <span class=\"comments\">\/* eliminate single digits *\/<\/span>\r\n        if( v &lt; 10 )\r\n        {\r\n            results[count] = v;\r\n            count++;\r\n        }\r\n        else\r\n        {\r\n            snprintf(fwd,size,\"%d\",v);\r\n            bkd = strrev(fwd);\r\n            if(strcmp(bkd,fwd) == 0 )\r\n            {\r\n                results[count] = v;\r\n                count++;\r\n            }\r\n        }\r\n        v++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* output the results in a fancy table *\/<\/span>\r\n    x = 0;\r\n    while( count )\r\n    {\r\n        for( y=0; y&lt;columns; y++ )\r\n        {\r\n            printf(\"%5d\",results[(x*columns)+y]);\r\n            count--;                <span class=\"comments\">\/* decrement the count *\/<\/span>\r\n            if( !count )            <span class=\"comments\">\/* if count is zero, stop *\/<\/span>\r\n                break;\r\n            if( y&lt;columns-1)\r\n                putchar('\\t');\r\n        }\r\n        x++;\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>A <em>while<\/em> loop counts the palindromic numbers, zero through 100. An <em>if<\/em> test handles single digit values <code>(v &lt; 10)<\/code> as these are all considered palindromic.<\/p>\n<p>The <em>else<\/em> portion of the decision uses the <em>snprintf()<\/em> function to convert integer <code>v<\/code> into a string <code>fwd[]<\/code>. The <em>strrev()<\/em> function reverses this string into a new string, <em>char<\/em> pointer <code>bkd<\/code>. The <em>strcmp<\/em> function compares the two values in an <em>if<\/em> decision. When they match, the number is palindromic. Its value is saved in array <code>results[]<\/code> and variable <code>count<\/code> is incremented.<\/p>\n<p>The <em>main()<\/em> function&#8217;s second <em>while<\/em> loop outputs the results in a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5511\">fancy table<\/a>. The output is shown in Figure 1.<\/p>\n<div id=\"attachment_5546\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5546\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/0101-figure1.png\" alt=\"screen output\" width=\"550\" height=\"210\" class=\"size-full wp-image-5546\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/0101-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/0101-figure1-300x115.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/10\/0101-figure1-500x191.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-5546\" class=\"wp-caption-text\">Figure 1. Output from my solution, using a table borrowed from last month&#8217;s solution.<\/p><\/div>\n<p>I&#8217;d be curious to see a mathematical solution for this exercise. I assume you could use the modulo operator or perhaps some logarithmic witchcraft to peel off digits from the original integer. But even if you could re-splice these values into a new, reversed integer, how do you test to determine whether the two values are palindromic? Do the eggheads have a solution?<\/p>\n<p>Regardless, I hope your solution, however it&#8217;s accomplished, met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to output the first 100 palindromic numbers, which are integers that reflect the same digits on either side. Rather than devise a complex mathematical equation to determine these values, I cheated.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-5557","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5557"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5557\/revisions"}],"predecessor-version":[{"id":5575,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5557\/revisions\/5575"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}