Switch case is used to select the operations from many statements. Here, a simple calculation program is given below.
First, get
the operation to performed.
Next, get
the two inputs.
Based on
the inputs, perform the operation and display the results.
Now ,the
program begins….
“calculate.c”
#include<stdio.h>
#include<conio.h>
void main()
{
char operation,
double a,b;
printf(“Enter
the operation: +, - , /, * , % ”);
scanf(“%c”,&operation);
printf(“Enter
the numbers”);
scanf(“ %lf
%lf”, &a,&b);
switch(operation)
{
case ‘+’ :
printf(“ %.1lf + %.1lf : %.1lf”,
a,b,a+b);
break;
case ‘-’ :
printf(“ %.1lf - %.1lf : %.1lf”,
a,b,a-b);
break;
case ‘/’ :
printf(“ %.1lf / %.1lf : %.1lf”,
a,b,a/b);
break;
case ‘*’ :
printf(“ %.1lf * %.1lf : %.1lf”,
a,b,a*b);
break;
case ‘%’ :
printf(“ %.1lf + %.1lf : %.1lf”,
a,b,a%b);
break;
default:
printf(“error, The operator is
not correct”);
break;
}
getch();
}
The output
will be like as follows…
Enter the
operation: +, - , /, * , % +
Enter the
numbers 3 4
3.0 + 4.0 =
7.0
No comments:
Post a Comment