# C Program to Take Operator as Input and Perform Arithmetic

In this article, we will learn How to take operator as input and perform arithmetic operations on two asked numbers (Integer or float) from the users.

To write this program, we need the very basic concept of arithmetic operations (Addition, subtraction, multiplication, and division – school mathematics actually), basic C programming concept (data type, I/O functions, etc)

## Algorithm – Operator as Input and Performing Arithmetic

- We will calculate all the things in the main function.
- Inside the main function, at first declare three float variables (The first two variables for taking two numbers from user num1 and num2, and the third input is the result for keeping the result after arithmetic operation between two numbers based on the operator input.
- Scan/ask for two float numbers (can be integer input also) from the user, keep them into num1 and num2 variables.
- Declare a char variable called operator for taking the arithmetic operator and keep that info operator variable.
- Ask for the operator using the scanf() function. Before that print an indicator using the printf() function.
- Write a total of 5 switch cases, four cases for checking the operator, and perform corresponding operations. And the rest one for throwing error.

## C Program to Take Operator as Input and Perform Arithmetic

```
#include <stdio.h>
int main() {
float num1, num2, result;
printf("Enter two numbers: ");
scanf("%f%f", &num1, &num2);
jump:
printf("Enter any one operator (+, -, *, /): ");
char operator;
scanf("%c", &operator);
operator = getchar();
switch(operator) {
case '+':
result = num1 + num2;
printf("Sum is: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Difference is: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Product is: %.2f\n", result);
break;
case '/':
result = num1 / num2;
printf("Quotient is: %.2f\n", result);
break;
default:
printf("Wrong operator input\n");
goto jump;
}
return 0;
}
```

- Copy the above code in a file and save with .c extension. Then run the program using your favorite IDE. You will get the output like the following:

Sample Input | Sample Output |
---|---|

Enter two numbers: 12 9 Enter any one operator (+, -, *, /): + |
Sum is: 21.00 |

Enter two numbers: 8 8 Enter any one operator (+, -, *, /): * |
Product is: 64.00 |

Enter two numbers: 9 2 Enter any one operator (+, -, *, /): $ |
Wrong operator input Enter any one operator (+, -, *, /): |

## General Learning Output (GLO)

- Declaring multiple float variables in a single statement.
- Taking multiple float inputs in a single scanf() function.
- Declaring char variable and taking character input in the variable.
- Syntax and uses of switch case in C programming.
- Uses of default switch case and it’s working mechanism.
- Uses of goto and jump statement in C programming language

- C program to print Integer & Decimal number given by user
- C Program to perform all arithmetic calculation
- C program to ask two numbers and find their sum
- C program to check a number is even, odd or zero
- C program to swap between two numbers
- C program to sum of natural numbers up to N
- C program to calculate the factorial of a number
- C program to make a multiplication table
- C program to calculate GCD of two numbers
- C program to calculate LCM of two numbers
- C program to count number of digits in a number
- C program to reverse a integer number
- C program to find the power of a number
- C program to check whether a number is palindrome or not
- C program to count and print prime numbers up to the N
- C program to check armstrong numbers
- C program to check armstrong numbers between two integers
- C program to find factors of a number
- C program to fined the size of all data types
- C program to print largest number among three numbers
- C program to check a number is positive or negetive
- C program to calculate the factorial of a number
- C program to form any kind of pyramid and stracture
- C program to find all the prime numbers between two numbers
- C program to check whether a number is prime or not
- C program to make a simple calculator
- C program to calculate the perimeter of a rectangle
- C program to calculate the area of a rectangle
- C program to calculate the diameter and area of a circle
- C program to convert kilometer to meter and centimeter
- C program to convert temperature celsius to fahrenheit
- C program to convert temperature fahrenheit to celsius
- C program to ask for days and convert into years, week and days
- C program to calculate the square root of a given number
- C program to find third angle of a triangle where two angles are given
- C program to calculate the area of a triangle
- C program to calculate area of an equilateral triangle
- C program to calculate average and percentage of result
- C program to calculate simple loan interest
- C program to compound loan interest
- C program to display fibonacci series of a number
- C program to swap two numbers without using third variable
- C program to print Hello World without using semicolon
- C program to insert assembly code and run it
- C program to printf Hello world without main() function
- C program to to calculate the multiplication of two matrix
- C program to convert decimal numbers into binary
- C program to form a triangle made with alphabet
- C program to form a triangle made with numbers
- C program to form a fibonacci triangle
- C program to convert a number into charecters

Happy Coding 🙂