Simple Calculator Using Switch Statement
In this blog, you will learn how to create a simple calculator, using switch statement.
Introduction
- In this blog, I am going to explain how to do simple calculations by using a calculator.
Software Requirements
Programming- #include < stdio.h >
- int main()
- {
- char operator;
- double firstNumber, secondNumber;
- printf("Enter an operator (+, -, *,): ");
- scanf("%c", & operator);
- printf("Enter two operands: ");
- scanf("%lf %lf", & firstNumber, & secondNumber);
- switch (operator)
- {
- case '+':
- printf("%.1lf + %.1lf = %.1lf", firstNumber, secondNumber, firstNumber + secondNumber);
- break;
- case '-':
- printf("%.1lf - %.1lf = %.1lf", firstNumber, secondNumber, firstNumber - secondNumber);
- break;
- case '*':
- printf("%.1lf * %.1lf = %.1lf", firstNumber, secondNumber, firstNumber * secondNumber);
- break;
- case '/':
- printf("%.1lf / %.1lf = %.1lf", firstNumber, secondNumber, firstNumber / firstNumber);
- break;
- default:
- printf("Error! operator is not correct");
- }
- return 0;
- }
Explanation