

{"id":116575,"date":"2023-08-21T19:08:16","date_gmt":"2023-08-21T13:38:16","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=116575"},"modified":"2023-08-21T19:08:39","modified_gmt":"2023-08-21T13:38:39","slug":"c-program-to-find-sum-of-digits-of-a-number","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/","title":{"rendered":"C Program to find Sum of Digits of a Number"},"content":{"rendered":"<p>In programming, there are often situations where you need to manipulate individual digits of a number. One common operation is to find the sum of the individual digits of a given number. In this article, we will explore a C program to achieve this task and discuss alternative approaches to solving it.<\/p>\n<h3>Ways to find Sum of digits of a Number in C<\/h3>\n<h4>Method 1:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\nint main() {\r\nint number, sum = 0, digit;\r\nprintf(\"Enter a number: \"); \r\nscanf(\"%d\", &amp;number);\r\nwhile (number != 0) \r\n{ \r\ndigit = number % 10; \r\nsum += digit;\r\nnumber \/= 10;\r\n}\r\nprintf(\"Sum of the individual digits: %d\\n\", sum);\r\nreturn 0;\r\n}\r\n<\/pre>\n<p>In the above program, we declare three variables: `number`, `sum`, and `digit`. The `number` variable is used to store the input number from the user. The `sum` variable is initialized to zero and will be used to accumulate the sum of the digits. The `digit` variable is used to store the individual digits extracted from the number.<\/p>\n<p>The program prompts the user to enter a number using the `printf` and `scanf` functions. Inside the `while` loop, we extract the last digit of the number using the modulo operator `%`. We then add the extracted digit to the `sum` variable. Finally, we divide the number by 10 to remove the last digit, and the process continues until the number becomes zero.<\/p>\n<p>Once the loop finishes, the program prints the sum of the individual digits using the `printf` function.<\/p>\n<h4>Method 2:<\/h4>\n<p>Now let&#8217;s consider an alternative approach to solving this problem using string manipulation. This method involves converting the number to a string and then iterating over the characters of the string to calculate the sum of the digits.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt; \r\n#include &lt;stdlib.h&gt; \r\n#include &lt;string.h&gt;\r\nint main() {\r\nchar number_str[20];\r\nint sum = 0;\r\nprintf(\"Enter a number: \");\r\nscanf(\"%s\", number_str);\r\nfor (int i = 0; i &lt; strlen(number_str); i++) {\r\nsum += number_str[i] - '0'; \r\n}\r\nprintf(\"Sum of the individual digits: %d\\n\", sum);\r\nreturn 0;\r\n}\r\n<\/pre>\n<p>In this alternative approach, we declare a character array `number_str` to store the number as a string. We also declare a variable `sum` to accumulate the sum of the digits.<\/p>\n<p>The program prompts the user to enter a number using the `printf` and `scanf` functions. We then iterate over each character of the string using a `for` loop. Inside the loop, we convert each character to its corresponding digit by subtracting the character `&#8217;0&#8217;`. This conversion works because in the ASCII character set, the characters `&#8217;0&#8217;` to `&#8217;9&#8217;` are consecutive. We add the converted digit to the `sum` variable.<\/p>\n<p>Finally, the program prints the sum of the individual digits using the `printf` function.<\/p>\n<p>Both the approaches discussed above yield the same result for finding the sum of the individual digits of a number. The choice between them depends on the specific requirements of your program or personal preference.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Enter a number: 123456<br \/>\nSum of the individual digits: 21<\/div>\n<h3>Conclusion:<\/h3>\n<p>In conclusion, we have explored two different approaches to finding the sum of the individual digits of a number in C. The first approach uses arithmetic operations to extract and manipulate digits, while the second approach converts the number to a string and operates on its characters. Both methods provide reliable ways to solve this problem, and you can choose the one that best suits your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In programming, there are often situations where you need to manipulate individual digits of a number. One common operation is to find the sum of the individual digits of a given number. In this&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":117875,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19488],"tags":[28005,28006],"class_list":["post-116575","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-sum-of-digits-of-a-number-in-c","tag-sum-of-digits-program-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C Program to find Sum of Digits of a Number - DataFlair<\/title>\n<meta name=\"description\" content=\"Explore two different ways to write program to find the sum of the individual digits of a number in C with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C Program to find Sum of Digits of a Number - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Explore two different ways to write program to find the sum of the individual digits of a number in C with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-21T13:38:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-21T13:38:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-for-addition-of-individual-digits-of-a-number.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C Program to find Sum of Digits of a Number - DataFlair","description":"Explore two different ways to write program to find the sum of the individual digits of a number in C with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/","og_locale":"en_US","og_type":"article","og_title":"C Program to find Sum of Digits of a Number - DataFlair","og_description":"Explore two different ways to write program to find the sum of the individual digits of a number in C with examples.","og_url":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-08-21T13:38:16+00:00","article_modified_time":"2023-08-21T13:38:39+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-for-addition-of-individual-digits-of-a-number.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"C Program to find Sum of Digits of a Number","datePublished":"2023-08-21T13:38:16+00:00","dateModified":"2023-08-21T13:38:39+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/"},"wordCount":472,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-for-addition-of-individual-digits-of-a-number.webp","keywords":["Sum of digits of a Number in C","sum of digits program in c"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/","url":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/","name":"C Program to find Sum of Digits of a Number - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-for-addition-of-individual-digits-of-a-number.webp","datePublished":"2023-08-21T13:38:16+00:00","dateModified":"2023-08-21T13:38:39+00:00","description":"Explore two different ways to write program to find the sum of the individual digits of a number in C with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-for-addition-of-individual-digits-of-a-number.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-for-addition-of-individual-digits-of-a-number.webp","width":1200,"height":628,"caption":"c program for addition of individual digits of a number"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-find-sum-of-digits-of-a-number\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"C Program to find Sum of Digits of a Number"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=116575"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116575\/revisions"}],"predecessor-version":[{"id":118625,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116575\/revisions\/118625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/117875"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=116575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=116575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=116575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}