C Online Compiler
Example: C program to make a simple calculator using switch statement
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C program to make a simple calculator using switch statement #include <stdio.h> #include <limits.h> int main() { char operator; double operand1 = 0, operand2 = 0; double result = 0; // INPUT the operator printf("Enter an operator from these - (+, -, *, /): "); scanf("%c", &operator); // INPUT the two values printf("\nEnter the two values to make calculation\n"); scanf("%lf %lf", &operand1, &operand2); switch (operator) { // Addition operation case '+': result = operand1 + operand2; break; // Subtraction operation case '-': result = operand1 - operand2; break; // Multiplication operation case '*': result = operand1 * operand2; break; // Division operation case '/': // Check for division by zero if (operand2 != 0) { result = operand1 / operand2; } else { printf("\nError! Division by zero.\n"); result = INT_MIN; } break; default: printf("\nError! Operator is not correct.\n"); result = INT_MIN; break; } printf("\nResult: %.2lf\n", result); return 0; }
- 90 35
Output
Clear
ADVERTISEMENTS