Arithmetic operations using switch case and do while loop in C Programming

A switch case is used to select one among multiple options.

Can we use arithmetic operators in switch case:

Yes, We Can use we. To provide symbols in a case statement a single quote is used.

To perform arithmetic operations, we create a case for each operator, for the matching operator, it will call the appropriate case statements and execute the statements.

In our program, we will use a switch case statement and do while loop statement

You can check both details here

  1. Switch Case Statement
  2. Do While Loop Statement

algorithm for arithmetic operations using switch case in c

Lets see steps to write this C program

  1. Declare variables
  2. start loop
  3. get num1 ,operator, num2 value from user
  4. start switch case and pass operator in switch case
  5. write the case for arithmetic operators and their associated statements
  6. define default in switch case
  7. Take input from the user to check loop continuity
  8. if the user enters yes then repeat steps 2-7 else end the loop.

switch case arithmetic operations in c

The following image explains the switch case arithmetic operations in c. We passed a character c in switch case expression and provided symbols in the case statement.

Also provided a default statement if it doesn’t match with any case.

Arithmetic Operations Using Switch Case in C Programming
Fig: Arithmetic Operations Using Switch Case in C Programming

C program for arithmetic operations using switch case

#include
int main() {
    int num1,num2;
    char c,ch;
    do{
       printf("\nEnter Num1 Operator [+,-,*,/] Num2 \n");
       scanf("%d %c %d",&num1,&c,&num2);
    switch(c){
        case '+':
                 printf("\n%d+%d=%d",num1,num2,num1+num2);
                 break;
        case '-':
                 printf("\n%d-%d=%d",num1,num2,num1-num2);
                 break;
        case '*':
                 printf("\n%d*%d=%d",num1,num2,num1*num2);
                 break;
        case '/':
                 printf("\n%d/%d=%f",num1,num2,(float)num1/num2);
                 break;
        default:
                 printf("\nEnter valid operation : +, -, *, /");
    }
    printf("\nEnter y to continue other character to end\n");
    getchar();
	scanf("%c",&ch);
    }while(ch=='y');
}
  1. Two Numbers and a character are taken from user and stored in variable num1 and num2 in variable c.
  2. Based on variable c we are deciding which operation to perform.
  3. variable c is passes to switch expression
  4. based on value it is selecting case statement.
  5. The entire process is inside do while loop
  6. If your press y then this loop continue

Output

 
Enter Num1 Operator [+,-,*,/] Num2
2 + 2

2+2=4
Enter y to continue other character to end
y

Enter Num1 Operator [+,-,*,/] Num2
3 * 2

3*2=6
Enter y to continue other character to end
y

Enter Num1 Operator [+,-,*,/] Num2
 5 - 1

5-1=4
Enter y to continue other character to end
y

Enter Num1 Operator [+,-,*,/] Num2
5 / 6

5/6=0.833333
Enter y to continue other character to end
y

Enter Num1 Operator [+,-,*,/] Num2
1 + 2

1+2=3
Enter y to continue other character to end
n

write a c program to perform arithmetic operations using switch case

#include
int main() {
    int num1,num2;
    int c;
    char ch;
    do{
       printf("\nEnter 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo \n");
       scanf("%d %d %d",&c,&num1,&num2);
    switch(c){
        case 1:
                 printf("\n%d+%d=%d",num1,num2,num1+num2);
                 break;
        case 2:
                 printf("\n%d-%d=%d",num1,num2,num1-num2);
                 break;
        case 3:
                 printf("\n%d*%d=%d",num1,num2,num1*num2);
                 break;
        case 4:
                 printf("\n%d/%d=%f",num1,num2,(float)num1/num2);
                 break;
        case 5:
                 printf("\n%d %% %d=%d",num1,num2,num1%num2);
                 break;
        default:
                 printf("\nEnter valid operation [1-5]");
    }
    printf("\nEnter y to continue other character to end\n");
    getchar();
	scanf("%c",&ch);
    }while(ch=='y');
}
 
Enter 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo 

10+20=30
Enter y to continue other character to end

Enter 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo 

10-4=6
Enter y to continue other character to end

Enter 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo 

2*2=4
Enter y to continue other character to end

Enter 1. Addition 2. Substraction 3. Multiplication 4. Division 5. Modulo 

10/2=5.000000
Enter y to continue other character to end

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Command Line Argument
  5. Local vs Global Object in C++
  6. Local and Nested Classes in C++
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++