11:54 PM 2009-2010 question 3 |
write a program perform the addition, subtraction, division or multiplication ,, the inputs to the program are two float values and arithmetic operator .use
1- if-else
2- switch
if-else :
#include<stdio.h>
main()
{
float x, y, result;
char op;
printf("enter the operator : ");
scanf("%c",&op);
printf("enter two values : ");
scanf("%f%f",&x, &y);
if(op=='+'){
result=x+y;
printf("sum = %4.2f", result);
}else if(op=='-'){
result=x-y;
printf("sub = %4.2f", result);
}else if(op=='*'){
result=x*y;
printf("multi = %4.2f", result);
}else if(op=='/'){
result=x/y;
printf("div = %4.2f", result);
}else
printf("wrong operator");
getch();
return 0;
}
switch :
#include<stdio.h>
main()
{
float x, y, result;
char op;
printf("enter the operator : ");
scanf("%c",&op);
printf("enter two values : ");
scanf("%f%f",&x, &y);
switch(op){
case '+':
result=x+y;
printf("sum = %4.2f", result);
break;
case '-':
result=x-y;
printf("sub = %4.2f", result);
break;
case '*':
result=x*y;
printf("multi = %4.2f", result);
break;
case '/':
result=x/y;
printf("div = %4.2f", result);
break;
default:
printf("wrong operator");
break;
}
getch();
return 0;
}
|
|
Total comments: 0 | |