C Program to make Calculator using switch case

Share article on social media
Here is an article on how to create a simple calculator program using a switch case
statement in C:
Creating a Calculator Program in C using a Switch case Statement
Calculator programs are a common project for beginners to learn a programming language. In this article, we will create a simple calculator program in C using a switch case
statement.
To begin, we will first need to include the stdio.h
header file, which provides functions for input and output in C.
#include
Next, we will define the main
function where the program will start execution. Inside the main
function, we will declare variables for the operator and operands. We will use a char
type for the operator and double
types for the operands.
int main() { char operator; double firstNumber, secondNumber;
Next, we will prompt the user to enter an operator by using the printf
function and then use the scanf
function to read the operator entered by the user.
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
Then, we will prompt the user to enter two operands and read them using the scanf
function.
printf("Enter two operands: ");
scanf("%lf %lf", &firstNumber, &secondNumber);
Now, we will use a switch
statement to perform the corresponding operation based on the operator entered by the user. The switch
statement will have cases for each of the four possible operators: +
, -
, *
, and /
. For each case, we will use the printf
function to print the result of the operation.
switch(operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", firstNumber, secondNumber, firstNumber * secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", firstNumber, secondNumber, firstNumber / secondNumber);
break;
Finally, we will include a default case for when the operator entered by the user does not match any of the cases. This will handle any invalid input by the user.
// operator doesn't match any case default: printf("Error! operator is not correct"); }
To finish the program, we will return 0 from the main
function to indicate that the program has successfully completed.
return 0; }
Here is an example of a simple calculator program using a switch case
statement in C:
#include
int main() {
char operator;
double firstNumber,
secondNumber;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &firstNumber, &secondNumber);
switch(operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", firstNumber, secondNumber, firstNumber * secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", firstNumber, secondNumber, firstNumber / secondNumber);
break;
//operator doesnt match any case
default:
printf("Error! operator is not correct");
}
return 0;
}
;