C Program to perform arithmetic operations using Switch and If else Statement


Program Description:

In this article, We are going to write a C Program to perform arithmetic operations using the Switch statement and if else ladder.

The Program should accept Two Numbers and One Operator from the user and perform the arithmetic operations like Addition, Subtraction, Multiplication, Division, and Modulo division on provided numbers. The program should also do the error check and display the error if the user enters the Invalid Input.

Let’s look at a couple of example Inputs and Outputs

Excepted Input and Output:

Example 1:


Ask the two numbers and Operator from the user.

Enter two Number : 500 1000

Enter the Operator [ +, -, *, /, %] : +


Addition: 500 + 1000 = 1500

Example 2:


Enter two Number : 25 5

Enter the Operator [ +, -, *, /, %] : /


Division: 25 / 5 = 5

Example 3: Invalid Input case:


Enter two Number : 60 20
Enter the Operator [ +, -, *, /, %] : &

ERROR: Invalid Operation, Please try again

Program Pre-Requisites:

It is recommended to have knowledge of the switch statement and break statement and arithmetic operations on C. Please go through the following articles to learn more about them.

Program to Perform Arithmetic Operations:

We can write the program using the switch statement and the if else statement. Let’s look at both methods in this program.

Method 1: Perform arithmetic operations using the switch statement

Method 2: Perform arithmetic operations using the if else ladder.

Method 1: C Program to Perform arithmetic operations Algorithm using Switch Statement:

  1. The Program will start by taking the two numbers from the user and storing them in the variables x and y.
  2. Then we also ask for the Operator, i.e Addition, Subtraction, etc.
    • Users need to specify the + for Addition
    • - for Subtraction
    • * for product or multiplication
    • / for division
    • % for modulo division
  3. Once we got the operator, We are storing it in a variable called op
  4. Then We are using the switch statement to compare the operator op with available options ( +, -, etc) using switch (op) condition.
  5. If the user selects the Addition i.e + as the Operation, Then the first case case '+': will be executed and we are calculating the addition and printing output to the console. Make sure to break out of the loop using the break statement.
  6. Similarly, If the user selects Multiplication operator (*) Then the case '*': will be executed.
  7. This process continues based on User Input.
  8. If the user enters Invalid Numbers or the Invalid Operator, Then the Switch statement’s default case will be executed. Which displays the Error Message ERROR: Invalid Operation, Please try again Then we are using the goto INPUT to take the control back to the top of the program. The Goto statement is used to jump from one statement in the program to another.

Let’s convert the above pseudo code into the C program.

Program: C Program to Perform arithmetic operations Algorithm using Switch Statement:

Program Output:

We are using the GCC Compiler to compile and run the program.

$ gcc arithmetic-operations-switch.c

The gcc compiler by default generates the executable file with the a.out Name. So we are going to run the a.out using the ./a.out command. ( As we are in the same directory as where the file is located )

Example 1: Addition test case:

Example 2: Subtraction:

More Tests: Example 3: Multiplication, Division, and Modulo division:

Example 4: If user enters the wrong number:

Example 5: If the user enters the wrong Operator:

Method 2: C Program to Perform arithmetic operations Algorithm using if else ladder Statement:

The Program logic is the same as the above method, We are going to take the two numbers and the operator from the user. Now instead of using the switch statement, We are going to use the if else ladder.

So We are going to compare the operator op with arithmetic operations ( addition, subtraction, etc) using the condition like if(op == '+') , if (op == '-'), etc.

If the op is not equal to any of the operations, Then the final else statement will be executed, where we are handling the Invalid Input case.

Program Arithmetic Operations with if else ladder:

Program Output:

As you can see from the above output, The program is able to do the error check as well and display the Error message on Invalid input.

  1. C Program to check character is Alphabet or Number of Special Number
  2. C Program to Calculate the Minimum of Three Numbers
  3. C Program to find the Biggest number from Three Numbers (Maximum)
  4. C Program to calculate the Roots of a Quadratic Equation
  5. C Program to Calculate the Remainder and Quotient

C Tutorials Index:


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Perform arithmetic Operations using Switch Statement […]

Leave a Reply