case statement in c example
Example 1: switch statement in c
#include <stdio.h>
int main(void)
{
int a = 0;
switch(a)
{
case 1 :
statement("a = 1");
break;
case 2 :
printf("a = 2");
break;
default :
printf("a is neither 1 or 2");
break;
}
}
Example 2: switch case in c
#include <stdio.h>
int main() {
char operator;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}