belongs to collection: Switch case C programs with an examples
Write C program to create calculator using switch Statement
# include <stdio.h> int main() { char operator; double firstNumber,secondNumber; //Reading operator from user printf("Enter an operator (+, -, *,): "); scanf("%c", &operator); //Reading operands from user 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 / secondNumber); break; // operator doesn't match any case constant (+, -, *, /) default: printf("Error! please enter correct operator"); } return 0; }
Result:
Enter an operator (+, -, *,): *
Enter two operands: 12
60
12.0 * 60.0 = 720.0
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Result:
Enter an operator (+, -, *,): *
Enter two operands: 12
60
12.0 * 60.0 = 720.0
need an explanation for this answer? contact us directly to get an explanation for this answer