Subscribe

RSS Feed (xml)

Tuesday, January 6, 2009

Write a C program which takes integer operands and one operator from the user, performs the operation and then prints the result.

// Consider the operators +, -, *, / and use switch statement.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int x, y;
char ch;
clrscr( );
printf("Enter the two integer values:");
scanf("%d %d",&x, &y);
printf("\nEnter the required operator( +, -, *, / ): ");
fflush(stdin); //to flush [clear] the input buffer.
scanf("%c", &ch);
switch(ch)
{
case '+' : printf("\n Addition : %d", x+y); break;
case '-' : printf("\n Substraction : %d", x-y); break;
case '*' : printf("\n Multipliaction : %d", x*y); break;
case '/': printf("\n Division: %d", x/y); break;
default: printf("\nInvalid operator, please check once again.");
}
getch();
return 0;
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

No comments:

Post a Comment