C program for switch case to perform add mul sub and div operation

C program for switch case to perform add mul sub and div operation. The control statement that allows us to make a decision from the number of choices is called a switch. Let us see how this program works and how switch case is used, and how different cases are made for performing different operation according to the need.
Switch case is widely use while implementing data structure program, as we make different code for different operation like, pop, push or insert, delete etc. You can look for the following c program code too which uses switch case :

  • c program to implement queue using stack
  • c program to implement dequeue (input-restricted and output-restricted)
  • c program to implmenent circular queue
  • C program to implement queue using linked list
  • C program to implement queue using array

. There are many more program which uses switch case.

C program for switch case to perform add mul sub and div operation

 

#include<stdio.h>
#include<conio.h>

int main()
{
    int choice, a, b;
    int sum, div, mul, sub;
    char c= 'y';

    while(c=='y')
    {
         printf("\nAddition  -> 1\nSubtraction  -> 2 \nMultiplication  -> 3 \nDivision  -> 4\n");
         printf("\aEnter your choice of operation to perform -: \n");
         scanf("%d",& choice);
         printf("\a\nEnter two number :- \n");
         scanf("%d%d",&a,&b);
         switch(choice)
         {

              case 1: sum = a + b;
              printf("\a\n Sum = %d", sum);
              break;

              case 2: sub = a - b;
              printf("\a\n Difference = %d", sub);
              break;

              case 3: mul = a * b;
              printf("\a\n Multiplication = %d", mul);
              break;

              case 4: div = a - b;
              printf("\a\n Division = %d", div);
              break;

              default : printf("\aWRONG CHOICE");
        }
        printf("\nDo you want to continue [y / n]");
        c=getch();
    }
    getch();
}

Output of C program for switch case:

c program for switch case , perform add, mul, sub and div operation
c program to use switch case

 


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>