[My site ]
Main » 2013 » December » 12 » 2009-2010 question 3
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;
}



Category: C Programming - 1 | Views: 411 | Added by: ahansaary | Rating: 0.0/0
Total comments: 0
Only registered users can add comments.
[ Sign Up | Login ]