{"id":25832,"date":"2017-10-25T20:50:28","date_gmt":"2017-10-25T15:20:28","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25832"},"modified":"2017-10-25T20:50:28","modified_gmt":"2017-10-25T15:20:28","slug":"c-programming-check-divisibility-7","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-check-divisibility-7\/","title":{"rendered":"C Programming &#8211; Check divisibility by 7"},"content":{"rendered":"<p>Given a number, check if it is divisible by 7. You are not allowed to use modulo operator, floating point arithmetic is also not allowed.<span id=\"more-24601\"><\/span><\/p>\n<p>A simple method is repeated subtraction. Following is another interesting method.<\/p>\n<p>Divisibility by 7 can be checked by a recursive method. A number of the form 10a + b is divisible by 7 if and only if a \u2013 2b is divisible by 7. In other words, subtract twice the last digit from the number formed by the remaining digits. Continue to do this until a small number.<\/p>\n<p><strong>Example:<\/strong> the number 371: 37 \u2013 (2\u00d71) = 37 \u2013 2 = 35; 3 \u2013 (2 \u00d7 5) = 3 \u2013 10 = -7; thus, since -7 is divisible by 7, 371 is divisible by 7.<\/p>\n<p>Following is C implementation of the above method<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A Program to check whether a number is divisible by 7<br\/>#include &lt;stdio.h&gt;<br\/> <br\/>int isDivisibleBy7( int num )<br\/>{<br\/>    \/\/ If number is negative, make it positive<br\/>    if( num &lt; 0 )<br\/>        return isDivisibleBy7( -num );<br\/> <br\/>    \/\/ Base cases<br\/>    if( num == 0 || num == 7 )<br\/>        return 1;<br\/>    if( num &lt; 10 )<br\/>        return 0;<br\/> <br\/>    \/\/ Recur for ( num \/ 10 - 2 * num % 10 ) <br\/>    return isDivisibleBy7( num \/ 10 - 2 * ( num - num \/ 10 * 10 ) );<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int num = 616;<br\/>    if( isDivisibleBy7(num ) )<br\/>        printf( &quot;Divisible&quot; );<br\/>    else<br\/>        printf( &quot;Not Divisible&quot; );<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Divisible<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>How does this work?<\/strong> Let \u2018b\u2019 be the last digit of a number \u2018n\u2019 and let \u2018a\u2019 be the number we get when we split off \u2018b\u2019.<br \/>\nThe representation of the number may also be multiplied by any number relatively prime to the divisor without changing its divisibility. After observing that 7 divides 21, we can perform the following:<\/p>\n<pre> 10.a + b<\/pre>\n<p>after multiplying by 2, this becomes<\/p>\n<pre> 20.a + 2.b<\/pre>\n<p>and then<\/p>\n<pre> 21.a - a + 2.b<\/pre>\n<p>Eliminating the multiple of 21 gives<\/p>\n<pre> -a + 2b<\/pre>\n<p>and multiplying by -1 gives<\/p>\n<pre> a - 2b<\/pre>\n<p>There are other interesting methods to check divisibility by 7 and other numbers.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Check divisibility by 7 &#8211; Mathematical Algorithms &#8211; Divisibility by 7 can be checked by a recursive method. A number of the form 10a + b.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,74058],"tags":[75584,75592,75575,75589,75599,75597,70865,75600,75587,75572,75578,75591,75598,75593,75588,74064,75570,75571,75594,75192,72110,75215,75254,75577,74082,75212,75583,75590,74093,74059,75579,75586,75581,75601,75569],"class_list":["post-25832","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-mathematical-algorithms","tag-c-programming-modulus","tag-divisibility-rules-for-13","tag-divisibility-rules-for-3","tag-divisibility-rules-for-7-and-8","tag-divisibility-rules-worksheets-grade-7","tag-divisibility-test-of-8","tag-even-odd-program-in-c","tag-examples-of-divisibility-rules-for-7","tag-factors-of-a-number","tag-first-4-prime-numbers","tag-first-five-prime-numbers","tag-how-to-find-multiples-of-a-number","tag-math-divisible","tag-numbers-divisible-by-2","tag-numbers-divisible-by-4","tag-numbers-divisible-by-7","tag-odd-even-program-in-c","tag-odd-or-even-program-in-c","tag-prime-factors-of-10","tag-prime-no-program-in-c","tag-prime-number-program-in-c","tag-prime-numbers-from-1-to-100-in-c","tag-prime-numbers-from-1-to-100-in-java","tag-prime-numbers-from-1-to-20","tag-program-for-prime-number-in-c","tag-program-to-find-prime-numbers","tag-smallest-2-digit-number","tag-smallest-4-digit-number","tag-smallest-even-number","tag-smallest-three-digit-number","tag-test-of-divisibility-by-7","tag-test-of-divisibility-of-7","tag-three-digit-prime-numbers","tag-what-does-divisible-mean-in-math","tag-what-is-the-prime-factorization-of-50"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25832"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25832\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}