{"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[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20A%20Program%20to%20check%20whether%20a%20number%20is%20divisible%20by%207%0A%23include%20%3Cstdio.h%3E%0A%20%0Aint%20isDivisibleBy7(%20int%20num%20)%0A%7B%0A%20%20%20%20%2F%2F%20If%20number%20is%20negative%2C%20make%20it%20positive%0A%20%20%20%20if(%20num%20%3C%200%20)%0A%20%20%20%20%20%20%20%20return%20isDivisibleBy7(%20-num%20)%3B%0A%20%0A%20%20%20%20%2F%2F%20Base%20cases%0A%20%20%20%20if(%20num%20%3D%3D%200%20%7C%7C%20num%20%3D%3D%207%20)%0A%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20if(%20num%20%3C%2010%20)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Recur%20for%20(%20num%20%2F%2010%20-%202%20*%20num%20%25%2010%20)%20%0A%20%20%20%20return%20isDivisibleBy7(%20num%20%2F%2010%20-%202%20*%20(%20num%20-%20num%20%2F%2010%20*%2010%20)%20)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20num%20%3D%20616%3B%0A%20%20%20%20if(%20isDivisibleBy7(num%20)%20)%0A%20%20%20%20%20%20%20%20printf(%20%22Divisible%22%20)%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20printf(%20%22Not%20Divisible%22%20)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>Divisible<\/pre>\n[ad type=\u201dbanner\u201d]\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=\u201dbanner\u201d]\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}]}}