Write a program to make a simple calculator using switch-case. The calculator takes the operation (+ or – or * or /) and takes the two input arguments and print the results.
#include <stdio.h>
float calc(float a, float b, char oper)
{
float rez;
switch(oper){
case '+':
rez = a + b;
break;
case '-':
rez = a - b;
break;
case '*':
rez = a * b;
break;
case '/':
rez = a / b;
break;
}
return rez;
}
int main()
{
char oper;
float a, b;
printf("Enter argument 1: ");
scanf("%f", &a);
printf("Enter argument 2: ");
scanf("%f", &b);
getchar();
printf("Enter operation (+ or - or * or /): ");
oper = fgetchar();
getchar();
printf("\nResult:\n");
printf("\n%.1f %c %.1f = %.2f", a, oper, b, calc(a, b, oper));
}
Comments
Leave a comment