Q.8 Simulate calculator using switch statement. (AKTU. 2009-10)
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int flag = 0;
float a, b, c;
char choice;
while(flag == 0)
{
clrscr();
printf("\nCalculator");
printf("\nPress 1 for ADDITION");
printf("\nPress 2 for SUBSTRACTION");
printf("\nPress 3 for MULTIPLICATION");
printf("\nPress 4 for DIVISION");
printf("\nPress any other key to exit.");
choice = getche();
switch(choice)
{
case '1' :
printf("\nADDITION");
printf("\n Enter the value of a\n");
scanf("%f",&a);
printf("\n Enter the value of b\n");
scanf("%f",&b);
c = a + b;
printf("\n SUM = %f", c);
getch();
break;
case '2' :
printf("\nSUBSTRACTION");
printf("\n Enter the value of a\n");
scanf("%f",&a);
printf("\n Enter the value of b\n");
scanf("%f",&b);
c = a - b;
printf("\n DIFFERENCE a - b = %f", c);
getch();
break;
case '3' :
printf("\nMULTIPLICATION");
printf("\n Enter the value of a\n");
scanf("%f",&a);
printf("\n Enter the value of b\n");
scanf("%f",&b);
c = a * b;
printf("\n PRODUCT = %f", c);
getch();
break;
case '4' :
printf("\nDIVISON");
printf("\n Enter the value of a\n");
scanf("%f",&a);
printf("\n Enter the value of b\n");
scanf("%f",&b);
if((b == 0)&&( a < 0))
printf("\n - infinity");
else if((b == 0)&&( a > 0))
printf("\n + infinity");
else
{
c = a / b;
printf("\n QUOTIENT = %f", c);
}
getch();
break;
default :
flag = 1;
break;
}/* end switch case*/
}/*end while*/
}
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int flag = 0;
float a, b, c;
char choice;
while(flag == 0)
{
clrscr();
printf("\nCalculator");
printf("\nPress 1 for ADDITION");
printf("\nPress 2 for SUBSTRACTION");
printf("\nPress 3 for MULTIPLICATION");
printf("\nPress 4 for DIVISION");
printf("\nPress any other key to exit.");
choice = getche();
switch(choice)
{
case '1' :
printf("\nADDITION");
printf("\n Enter the value of a\n");
scanf("%f",&a);
printf("\n Enter the value of b\n");
scanf("%f",&b);
c = a + b;
printf("\n SUM = %f", c);
getch();
break;
case '2' :
printf("\nSUBSTRACTION");
printf("\n Enter the value of a\n");
scanf("%f",&a);
printf("\n Enter the value of b\n");
scanf("%f",&b);
c = a - b;
printf("\n DIFFERENCE a - b = %f", c);
getch();
break;
case '3' :
printf("\nMULTIPLICATION");
printf("\n Enter the value of a\n");
scanf("%f",&a);
printf("\n Enter the value of b\n");
scanf("%f",&b);
c = a * b;
printf("\n PRODUCT = %f", c);
getch();
break;
case '4' :
printf("\nDIVISON");
printf("\n Enter the value of a\n");
scanf("%f",&a);
printf("\n Enter the value of b\n");
scanf("%f",&b);
if((b == 0)&&( a < 0))
printf("\n - infinity");
else if((b == 0)&&( a > 0))
printf("\n + infinity");
else
{
c = a / b;
printf("\n QUOTIENT = %f", c);
}
getch();
break;
default :
flag = 1;
break;
}/* end switch case*/
}/*end while*/
}