2/21/2009

C Program To Perform Arithmetic Operation Using Switch Statement



    /*Write a c program to perform arithmetic
      operation using switch statement  */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b, sum=0,product=0,diff=0,division=0,op;
    printf("Enter the two numbers :\n");
        scanf("%d%d",&a,&b);
    printf("1.Addition\t2.Subtraction\t3.Multiplication\t4.Division");
    printf("\nEnter the choice :\n");
        scanf("%d",&op);
       switch(op)
            {
                case 1: sum=a+b;
                        printf("%d + %d = %d \n",a,b,sum);
                        break;
                case 2: diff=a-b;
                        printf("%d - %d = %d \n",a,b,diff);
                        break;
                case 3: product=a*b;
                        printf("%d * %d = %d \n",a,b,product);
                        break;
                case 4: division=a/b;
                        printf("%d / %d = %d \n",a,b,division);
                        break;
                default :
                        printf("Invalid choice\n");
                        break;

            }

    return 0;
}
Out Put:
Enter the two numbers :
5
4
1.Addition      2.Subtraction   3.Multiplication        4.Division
Enter the choice :
3
5 * 4 = 20

No comments:

Post a Comment