How to Take Operator as Input in C? Last Updated : 12 Jun, 2024 Comments Improve Suggest changes Like Article Like Report In C, an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. We may need to take operator as an input in some cases. In this article, we will learn how to take an operator as input in C. Get Input Operator in CTo take an operator as input in C, we can use the we can use any input function (scanf(), fgets(), etc) to take that operator as a character. We can them use any conditional statement (but switch statement is preferred in such cases) to find out which operator is used. C Program to Take Operator as InputThe below example demonstrates the use of scanf function to take an operator as input in C. C #include <stdio.h> int main() { char operator; double num1, num2, result; // Prompt the user to enter an operator printf("Enter an operator (+, -, *, /): "); scanf(" %c", &operator); // Prompt the user to enter two operands printf("Enter two numbers: "); scanf("%lf %lf", &num1, &num2); // Perform the operation based on the operator input switch (operator) { case '+': result = num1 + num2; printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result); break; case '-': result = num1 - num2; printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result); break; case '*': result = num1 * num2; printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result); break; case '/': if (num2 != 0) { result = num1 / num2; printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result); } else { printf("Error: Division by zero is not " "allowed.\n"); } break; default: printf("Error: Invalid operator.\n"); break; } return 0; } Output Enter an operator (+, -, *, /): +Enter two numbers: 102010.00 + 20.00 = 30.00We can define more operators in the switch statement and even include the check for binary, unary and ternary operator and then ask the operands accordingly. Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Take Operator as Input in C? D denzirop9v Follow Improve Article Tags : C Programs C Language C Examples Similar Reads How to Take Multiple Input in C? In C, we use scanf to take input from the user, and often it is necessary to take multiple inputs simultaneously. In this article, we will learn about how to take multiple inputs from the users in C. Multiple Inputs From a User in CTo take multiple inputs from users in C, we can declare an array to 2 min read How to Read Input Until EOF in C? In C, reading input until the End of the File (EOF) involves reading input until it reaches the end i.e. end of file. In this article, we will learn various methods through which we can read inputs until EOF in C.Reading Input Until EOFRead Input Until EOF Using getchar()Read Input Until EOF Using f 5 min read How to Write a Command Line Program in C? In C, we can provide arguments to a program while running it from the command line interface. These arguments are called command-line arguments. In this article, we will learn how to write a command line program in C. How to Write a Command Line Program in C? Command line arguments are passed to the 2 min read Program that allows integer input only Given an input value N, the task is to allow taking only integer input from the user. Now, if the user enters any input other than an integer, that is, a character or symbol, it will not be accepted by the program. Below is the C program to implement this approach: C // C program for the above appro 3 min read C Program to Add Two Integers Given two integers, the task is to add these integer numbers and return their sum.ExamplesInput: a = 5, b = 3Output: 8Explanation: The sum of 5 and 3 is 8.Input: a = -2, b = 7Output: 5Explanation: The sum of -2 and 7 is 5.In C, we have multiple methods to add two numbers, such as the addition operat 3 min read Like