A Simple Calculator Program in C
A simple calculator programmed using C programming language.
A simple calculator programmed using C programming language.
Write a program that acts like a calculator and performs the following operations:
The program must accept an operator from the user, and accordingly the operation needs to be performed.
Input:
Enter the operator (+, -, *, /): +
Enter the two numbers: 10 20
Output:
30.000000
Input:
Enter the operator (+, -, *, /): /
Enter the two numbers: 5 0
Output:
Division by zero is not allowed!
Input:
Enter the operator (+, -, *, /): -
Enter the two numbers: 35 40
Output:
-5.000000
You need to write the complete program using the following template as the starter:
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
// Write your code here
return 0;
}
Copy and paste the above code in your favorite IDE and start writing your code after the comment // Write your code here
.
Pro tip: First, try writing the code on your own. Try every possible thing you can to come up with the solution. If you still can’t figure it out, then check the solution and see what went wrong. This is how you learn. 🙂
To verify the program you have written, go ahead and test your program using the following test cases:
Input | Output | |
---|---|---|
Test Case 1 | + 45 30 | 75.000000 |
Test Case 2 | – 3.145 3.145 | 0.000000 |
Test Case 3 | / 10 0 | Division by zero is not allowed! |
Test Case 4 | / 10 20 | 0.500000 |
Don’t forget to try the code on your own before diving into the solution.
Following are the multiple approaches to solve the problem.
We can use a couple of if-else statements to decide the operation to be performed as shown in the following program:
#include <stdio.h>
#include <stdlib.h>
int main() {
char operator;
double num1, num2, result;
// Write your code here
printf("Enter the operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter the two numbers: ");
scanf("%lf%lf", &num1, &num2);
if (operator == '+') {
result = num1 + num2;
} else if (operator == '-') {
result = num1 - num2;
} else if (operator == '*') {
result = num1 * num2;
} else if (operator == '/') {
if (num2 != 0) {
result = num1 / num2;
} else {
// exit with FAILURE status
printf("Division by zero is not allowed!");
exit(1);
}
} else {
printf("Invalid operator.\n");
}
printf("%lf\n", result);
return 0;
}
Output:
Enter the operator (+, -, *, /): /
Enter the two numbers: 10 20
0.500000
We can use the Switch statement to decide the operation to be performed. Following program depicts the same:
#include <stdio.h>
#include <stdlib.h>
int main() {
char operator;
double num1, num2, result;
// Write your code here
printf("Enter the operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter the two numbers: ");
scanf("%lf%lf", &num1, &num2);
switch(operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
// Handling division by zero error
if (num2 != 0) {
result = num1 / num2;
break;
} else {
// exit with FAILURE status
printf("Division by zero is not allowed!");
exit(1);
}
default:
printf("Invalid operator.\n");
}
printf("%lf\n", result);
return 0;
}
Output:
Enter the operator (+, -, *, /): /
Enter the two numbers: 10 20
0.500000
You can check the above programs for multiple test cases and the code works just fine.
Leave a comment