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

  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

write a c program to perform arithmetic operations using switch case

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++