C Program to Perform Arithmetic Operations Using Switch

ALGORITHM:

  1. Begin
  2. Enter a, b values.
  3. Print ‘MENU’.
    (i) Print ‘+ Addition’.
    (ii) Print ‘- Subtraction’.
    (iii) Print ‘* Multiplication’.
    (iv) Print ‘/ Division’.
    (v) Print ‘% Remainder’.
    (vi) Print ‘E Exit’.
  4. Print ‘Enter your choice’.
  5. If op==’E’ then goto step 8 otherwise follow the below steps
  6. Switch(op)
    a. case +:
    i. Print ‘Addition’.
    ii. c=a+b.
    iii. Print ‘Sum=’c.
    iv. break
    b. case -:
    v. Print ‘Subtraction’.
    vi. c=a-b.
    vii. Print ‘Difference=’c.
    viii. break
    c. case : ix. Print ‘Multiplication’. x. c=ab.
    xi. Print ‘Product=’c.
    xii. break
    d. case /:
    xiii. Print ‘Division’.
    xiv. c=a/b.
    xv. Print ‘Quotient=’c.
    xvi. break
    e. case %:
    xvii. Print ‘Remainder’.
    xviii. c=a%b.
    xix. Print ‘Remainder=’c.
    xx. break
    f. default:
    xxi. Print ‘Invalid Option’.
    xxii. break
  7. while(1) then goto step 3.
  8. Stop.
    FLOW CHART is given for the same program in Flowcharts Section.

Program for Arithmetic Operations using SWITCH:

int main()
{
int a, b, c;
char op;
printf(“Enter a and b:”);
scanf(“%d%d”,&a,&b);
do
{
printf(“\n\nMENU\n”);
printf(“+ Addition\n”);
printf(“- Subtraction\n”);
printf(“* Multiplication\n”);
printf(“/ Division\n”);
printf(“%% Remainder\n”);
printf(“E Exit\n”);
printf(“Enter your choice :”);
getchar();
op=getchar();
if(op==’E’||op==’e’)
exit(1);
switch(op)
{
case ‘+’:
printf(“Addition\n”);
c=a+b;
printf(“Sum=%d\n”,c);
break;
case ‘-‘:
printf(“Subtraction\n”);
c=a-b;
printf(“Difference=%d\n”,c);
break;
case ‘‘: printf(“Multiplication\n”); c=ab;
printf(“Product=%d\n”,c);
break;

    case '/':
        printf("Division\n");
        c=a/b;
        printf("Quotient=%d\n",c);
        break;
    case '%':
        printf("Remainder\n");
        c=a%b;
        printf("Remainder=%d\n",c);
        break;
    default:
        printf("Invalid Option\n");
        break;
    }    /*end of switch statement*/
}
while(1);    /*End of while*/

return 0;

}/End of main function/

OUTPUT:
Enter a and b:10 20

MENU

  • Addition
  • Subtraction
  • Multiplication
    / Division
    % Remainder
    E Exit
    Enter your choice :*
    Multiplication
    Product=200

MENU

  • Addition
  • Subtraction
  • Multiplication
    / Division
    % Remainder
    E Exit
    Enter your choice :%
    Remainder
    Remainder=10

MENU

  • Addition
  • Subtraction
  • Multiplication
    / Division
    % Remainder
    E Exit
    Enter your choice :E

Leave a Reply

Your email address will not be published. Required fields are marked *