C Program to Making a Simple Calculator Using Switch Case

In C programming, the switch statement is a control flow statement that provides a way to handle multiple cases based on the value of an expression. It is commonly used when you have a variable or an expression and you want to execute different code blocks based on its value.

The basic syntax of the switch statement is as follows:

Syntax :

switch (expression)

{

case constant1:

// code to be executed if expression matches constant1

break;

case constant2:

// code to be executed if expression matches constant2

break; //

additional cases as needed

default:

// code to be executed if none of the cases match

}

Switch-case Calculator


A switch-case statement in C is often used to implement a simple calculator program. In this example, I’ll show you how to create a basic calculator that performs addition, subtraction, multiplication, and division based on user input.

Program :

#include<stdio.h>
#include<conio.h>
int main(){
float a,b;
char op;
printf(“Enter First Number = “);
scanf(“%f”,&a);
printf(“Enter Second Number = “);
scanf(“%f”,&b);
printf(“Enter any Operator = “);
scanf(“%s”,&op);
switch(op)
{
case ‘+’: printf(“You Select Sum : \n Sum of Two Numbers is = “);
printf(“%0.3f + %0.3f = %0.3f”,a,b,a+b);
break;
case ‘-‘: printf(“You Select Subtraction : \n Subtraction of Two Numbers is = “);
printf(“%0.3f – %0.3f = %0.3f”,a,b,a-b);
break;
case ‘*’: printf(“You Select Multiplication : \n Multiplication of Two Numbers is = “);
printf(“%0.3f * %0.3f = %0.3f”,a,b,a*b);
break;
case ‘/’: printf(“You Select Division : \n Division of Two Numbers is = “);
printf(“%0.3f / %0.3f = %0.3f”,a,b,a/b);
break;
default :
printf(“\nWrong Input!”);
}
return 0;
}

Output :

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Basket
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare
Verified by MonsterInsights