(C++) CPP Programs - 3
Friday, December 14, 2007
3. Write a C program to simulate a simple calculator that performs arithmetic operations like addition, subtraction, multiplication and division only on integers. Error message should be reported, if any attempt is made to divide by 0. Use switch statement.
/* Simple calculator*/
#include
#include
void main()
{
int a,b,sum,sub,mul,div,choice;
clrscr();
printf("Enter any two integer\n\a");
scanf("%d%d",&a,&b);
printf("1.addition\n2.subtraction\n3.multiplication\n4.division\n");
printf("Enter your choice\n\a");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("sum=%d\n\a",(a+b));
break;
case 2:printf("subtraction=%d\n",(a-b));
break;
case 3:printf("multiplication=%d\n",(a*b));
break;
case 4:
{
if(b==0)// to check whether 2nd operand is null
printf("Error!\n\a");
else
printf("Division=%d\n",(a/b));
break;
}
default:printf("Invalid choice\n");
}
getch();
}
0 comments:
Post a Comment