C Program<\/strong><\/p>\n\n\n\n\/\/ C Program to Check Whether a Number is Harshad Number or Not\n#include <stdio.h>\n\nint main()\n{\n int num, n, rem = 0, sum = 0;\n\n \/\/ Asking for input\n printf(\"Enter a number to check: \");\n scanf(\"%d\", &num);\n\n n = num;\n\n \/\/ Calculating the sum\n while (num > 0)\n {\n rem = num % 10;\n sum = sum + rem;\n num = num \/ 10;\n }\n\n \/\/ Check whether number is equal to the sum\n if (n % sum == 0)\n {\n printf(\"%d is a harshad number.\", n);\n }\n else\n {\n printf(\"%d is not a harshad number.\", n);\n }\n\n return 0;\n}<\/pre>\n\n\n\nOutput 1<\/strong><\/p>\n\n\n\nEnter a number to check: 45\n45 is a harshad number.<\/code><\/pre>\n\n\n\nOutput 2<\/strong><\/p>\n\n\n\nEnter a number to check: 33\n33 is not a harshad number.<\/code><\/pre>\n\n\n\n<\/span>How Does This Program Work?<\/strong><\/span><\/h2>\n\n\n\n int num, n, rem = 0, sum = 0;\n<\/pre>\n\n\n\nIn this program, we have declared four int data type variables named num<\/code>, n<\/code>, rem<\/code>, and sum<\/code>.<\/p>\n\n\n\n printf(\"Enter a number to check: \");\n scanf(\"%d\", &num);\n<\/pre>\n\n\n\nThen, the program prompts the user to enter a number. The scanf()<\/code> function is used to read the number entered by the user and store it in the num<\/code> variable.<\/p>\n\n\n\n n = num;\n<\/pre>\n\n\n\nWe store the value of the original number in the n<\/code> variable to use it later in the comparison.<\/p>\n\n\n\n while (num > 0)\n {\n rem = num % 10;\n sum = sum + rem;\n num = num \/ 10;\n }\n<\/pre>\n\n\n\nThen, we use a loop to calculate the sum of digits of the entered number. Suppose the user entered the number 45, then:<\/p>\n\n\n\nIteration<\/td> rem = num % 10<\/td> sum = sum + rem<\/td> num = num \/10<\/td><\/tr> 45 > 0<\/td> 45 % 10 = 5<\/td> sum = 0 + 5 = 5<\/td> num = 45 \/ 10<\/td><\/tr> 4 > 0<\/td> 4 % 10 = 4<\/td> sum = 5 + 4 = 9<\/td> num = 4 \/ 10<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\nThe sum will be 9.<\/p>\n\n\n\n
Now, we check whether the original number is divisible by the sum of digits or not.<\/p>\n\n\n\n
if (n % sum == 0)\n {\n printf(\"%d is a harshad number.\", n);\n }\n else\n {\n printf(\"%d is not a harshad number.\", n);\n }<\/pre>\n\n\n\n \nIf the original number is completely divisible by the sum of digits of the original number, then that number is a Harshad number.<\/li>\n\n\n\n If the original number is not completely divisible by the sum of digits, then the entered number is not a Harshad number.<\/li>\n<\/ul>\n\n\n\n<\/span>Conclusion<\/strong><\/span><\/h2>\n\n\n\nI hope after going through this tutorial, you understand how to check whether a number is a Harshad number or not using the C programming language.<\/p>\n\n\n\n
You can use the same logic to check for Harshad number in other programming languages.<\/p>\n","protected":false},"excerpt":{"rendered":"
If a number is divisible by the sum of its digits, then that number is a Harshad Number. For example, 144 is a Harshad number because 1 + 4 + 4 = 9, and 144 is divisible by 9. Some other examples of Harshad numbers are 18, 27, 54, 12, 156, 1729, and so on.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[294],"tags":[209],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/9831"}],"collection":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/comments?post=9831"}],"version-history":[{"count":10,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/9831\/revisions"}],"predecessor-version":[{"id":10067,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/9831\/revisions\/10067"}],"wp:attachment":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media?parent=9831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/categories?post=9831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/tags?post=9831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}