Sunday, May 5, 2013

Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result.(Consider the Operator +,-,*,/,% and use switch statement.


#include<stdio.h>
#include<conio.h>
main()
{
    int a,b;
    char operator;
    clrscr();
    printf("Enter A,B values:");
    scanf("%d %d",&a,&b);
    printf("Choose one of the operator\n");
    printf(" + for addition \n - for subtraction \n * for multiply \n / for division \n \%   modulus\n");
    fflush(stdin);
    scanf("%c",&operator);
    switch(operator)
    {
        case '+' : printf("sum %d + %d= %d",a,b,a+b);
                break;
        case '-' : printf("Subtraction %d - %d = %d",a,b,a-b);
                break;
        case '*' : printf("Multiplication is %d",a,b,a*b);
                break;
        case '/' : printf("Division is %d / %d = %f",a,b,(float)(a)/b);
                break;
        case '%' : printf("Modules %d % %d = %d",a,b,a%b);
                break;
        default : printf("Wrong action");
    }
getch();
}

output 
Enter A,B values:
2 5 
Choose one of the operator 
 + for addition
 - for subtraction
 * for multiply
 / for division
 %   modulus
+
sum 2 + 5= 7

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More