Algorithm

1. Prompt the user to enter an operator (+, -, *, /).
2. Accept two numbers as input (num1, num2).
3. Use a switch-case statement on the entered operator.
- Case '+':
- Display the result of num1 + num2.
- Case '-':
- Display the result of num1 - num2.
- Case '*':
- Display the result of num1 * num2.
- Case '/':
- If num2 is not zero:
- Display the result of num1 / num2.
- Else:
- Display an error message for division by zero.
- Default:
- Display an error message for an invalid operator.
4. End.

Code Examples

#1 Code Example-Ceate calculator using switch...case

```Code - C Programming```

``````/**
* C program to create Simple Calculator using switch case
*/

#include <stdio.h>

int main()
{
char op;
float num1, num2, result=0.0f;

/* Print welcome message */
printf("WELCOME TO SIMPLE CALCULATOR\n");
printf("----------------------------\n");
printf("Enter [number 1] [+ - * /] [number 2]\n");

/* Input two number and operator from user */
scanf("%f %c %f", &num1, &op, &num2);

/* Switch the value and perform action based on operator*/
switch(op)
{
case '+':
result = num1 + num2;
break;

case '-':
result = num1 - num2;
break;

case '*':
result = num1 * num2;
break;

case '/':
result = num1 / num2;
break;

default:
printf("Invalid operator");
}

/* Prints the result */
printf("%.2f %c %.2f = %.2f", num1, op, num2, result);

return 0;
}``````
Copy The Code &

Output

cmd
WELCOME TO SIMPLE CALCULATOR
----------------------------
Enter [number 1] [+ - * /] [number 2]
22 * 6
22.00 * 6.00 = 132.00

#2 Code Example-C Program to create calculator using switch...case and functions

```Code - C Programming```

``````/**
* C program to create simple calculator using switch case and functions
*/

#include <stdio.h>

/**
* Function declarations for calculator
*/
float sub(float num1, float num2);
float mult(float num1, float num2);
float div(float num1, float num2);

int main()
{
char op;
float num1, num2, result=0.0f;

/* Print welcome message */
printf("WELCOME TO SIMPLE CALCULATOR\n");
printf("----------------------------\n");
printf("Enter [number 1] [+ - * /] [number 2]\n");

/* Input two number and operator from user */
scanf("%f %c %f", &num1, &op, &num2);

switch(op)
{
case '+':
break;

case '-':
result = sub(num1, num2);
break;

case '*':
result = mult(num1, num2);
break;

case '/':
result = div(num1, num2);
break;

default:
printf("Invalid operator");
}

/* Print the result */
printf("%.2f %c %.2f = %.2f", num1, op, num2, result);

return 0;
}

/**
* Function to add two numbers
*/
{
return num1 + num2;
}

/**
* Function to subtract two numbers
*/
float sub(float num1, float num2)
{
return num1 - num2;
}

/**
* Function to multiply two numbers
*/
float mult(float num1, float num2)
{
return num1 * num2;
}

/**
* Function to divide two numbers
*/
float div(float num1, float num2)
{
return num1 / num2;
}``````
Copy The Code &

Output

cmd
WELCOME TO SIMPLE CALCULATOR
----------------------------
Enter [number 1] [+ - * /] [number 2]
22 * 6
22.00 * 6.00 = 132.00