{"id":2049,"date":"2016-08-08T00:01:05","date_gmt":"2016-08-08T07:01:05","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2049"},"modified":"2016-08-06T09:01:15","modified_gmt":"2016-08-06T16:01:15","slug":"your-card-is-valid-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2049","title":{"rendered":"Your Card is Valid &#8211; Solution"},"content":{"rendered":"<p>Implementing a written algorithm is something you do frequently as a C language programmer. You&#8217;re given a set of directions and your job is to translate that English into computer code &mdash; and make that code work.<br \/>\n<!--more--><br \/>\nFor this month&#8217;s exercise, the challenge was to apply the MOD 10 algorithm to a series of sample credit card numbers and report whether each is valid. As a review, here is the algorithm:<\/p>\n<p><strong><\/p>\n<ol>\n<li>Take all the digits in the card number, except for the final digit (the check digit) and double every other digit.<\/li>\n<li>If a digit is greater than 9 (two-digits wide), add the digits. So the value 14 (double 7) changes into 5.<\/li>\n<li>Add the digits together.<\/li>\n<li>Multiply the result by 9.<\/li>\n<li>Compare the last digit of the result with the check digit.<\/li>\n<\/ol>\n<p><\/strong><\/p>\n<p>For my solution, I used two <em>for<\/em> loops. The first processes elements of the <code>card[]<\/code> array. A second <em>for<\/em> loop tallies the digits to get the sum (Step 3). That&#8217;s followed by some math to calculate the result (Step 4) and compare it with the check digit (Step 5). A <em>switch-case<\/em> structure then examines the card number&#8217;s first digit to report the card type.<\/p>\n<p>First comes the <em>for<\/em> loop that tallies the digits:<\/p>\n<pre class=\"screen\">\r\n        <span class=\"comments\">\/* tally the digits *\/<\/span>\r\n        sum = 0;\r\n        for(d=0; d<15; d+=2)\r\n        {\r\n            <span class=\"comments\">\/* double every odd digit *\/<\/span>\r\n            odd = (card[c][d] - '0') * 2;\r\n            <span class=\"comments\">\/* Any values greater than 9, sum the digits *\/<\/span>\r\n            if( odd > 9)\r\n                odd = odd % 10 + 1;\r\n            <span class=\"comments\">\/* fetch the even digit *\/<\/span>\r\n            even = (card[c][d+1] - '0');\r\n            <span class=\"comments\">\/* Add all the digits together except check digit*\/<\/span>\r\n            if( d < 14 )\r\n                sum += odd + even;\r\n            else\r\n                sum += odd;\r\n        }<\/pre>\n<p><em>int<\/em> variable <code>sum<\/code> holds the digit tally. It's initialized to zero.<\/p>\n<p>The <em>for<\/em> loop works through every other (odd) digit; loop variable <code>d<\/code> is incremented by 2 with <code>d+=2<\/code> as the loop step value.<\/p>\n<p>Variable <code>odd<\/code> holds the result of a doubled digit in the card number. Character value <code>'0'<\/code> is subtracted from the character digit to obtain the integer value, which is then doubled. If the value of <code>odd<\/code> is greater than 9, a quick digit-sum is performed to make the results single-digit. The calculation is <code>odd&nbsp;%&nbsp;10&nbsp;+&nbsp;1<\/code>, which is sneaky, but it works.<\/p>\n<p>The <code>if( d < 14 )<\/code> statement tallies the digits in the card number, both the unchanged even digits and the doubled <code>odd<\/code> digits. This statement ensures that for the last iteration of the loop, only the final <code>odd<\/code> digit is added to the <code>sum<\/code>, not the check digit.<\/p>\n<p>After the loop is complete, digit tally is held in variable <code>sum<\/code>. The remainder of the algorithm is implemented as follows:<\/p>\n<pre class=\"screen\">\r\n        <span class=\"comments\">\/* multiply the tally by 9 *\/<\/span>\r\n        sum *= 9;\r\n        <span class=\"comments\">\/* compare the last digit of the result to the check digit *\/<\/span>\r\n        last_digit = sum % 10;\r\n        check_digit = card[c][15] - '0';\r\n        if( check_digit == last_digit)\r\n            printf(\"Valid \");\r\n        else\r\n            printf(\"Invalid \");<\/pre>\n<p>The <code>sum<\/code> is multiplied by 9, then the last digit is peeled off and stored in the <code>last_digit<\/code> variable. That value is compared with the final digit of the card number, stored in variable <code>check_digit<\/code>. The <em>if<\/em> statement displays the results.<\/p>\n<p>The next chunk of code displays the card type. To view that chunk, <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/08exercise.c\">click here<\/a> to examine my full solution.<\/p>\n<p>Here is the program's sample output:<\/p>\n<pre><code>4485113397627032: Valid Visa\r\n6011621789921290: Valid Discover\r\n5542619587695576: Invalid Mastercard\r\n5107425739448074: Valid Mastercard\r\n4556114306308858: Valid Visa<\/code><\/pre>\n<p>If your programming journey arrived at the same destination, and your code was able to verify the card numbers as shown above, then great! Many solutions exist to solve programming puzzles and mine is only one of them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implementing a written algorithm is something you do frequently as a C language programmer. You&#8217;re given a set of directions and your job is to translate that English into computer code &mdash; and make that code work.<\/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-2049","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\/2049","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=2049"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2049\/revisions"}],"predecessor-version":[{"id":2075,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2049\/revisions\/2075"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}