C Program to Make a Simple Calculator
We can make a simple calculator using C language by two approaches using switch case and if-else statement.
Input:
7+8 8-9 8*7 8/3
Output:
Enter an operator (+, -, *, /), if want to exit press x: + Enter two first and second operand: 7 8 7.0 + 8.0 = 15.0 Enter an operator (+, -, *, /), if want to exit press x: - Enter two first and second operand: 8 9 8.0 - 9.0 = -1.0 Enter an operator (+, -, *, /), if want to exit press x: * Enter two first and second operand: 8 7 8.0 * 7.0 = 56.0 Enter an operator (+, -, *, /), if want to exit press x: / Enter two first and second operand: 8 3 8.0 / 3.0 = 2.7 Enter an operator (+, -, *, /), if want to exit press x: x
Using Switch Case:
Approach:
- We will do all the following steps inside a never-ending loop so that the calculator program keeps on working.
- Take input of operator and then operands.
- Check whether the user wants to quit the program if yes then quit it, for this, we can use a special character and tell the user about it like here we used “x”.
- Using switch case check operators and do operations accordingly.
C
// C Program to Make a Simple Calculator// Using switch case#include <stdio.h>#include <stdlib.h>int main(){ char ch; double a, b; while (1) { printf("Enter an operator (+, -, *, /), if want to exit press x: "); scanf(" %c", &ch;); // to exit if (ch == 'x') exit(0); printf("Enter two first and second operand: "); scanf("%lf %lf",&a;,&b;); // Using switch case we will differentiate // operations based on different operator switch (ch) { // For Addition case '+': printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b); break; // For Subtraction case '-': printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b); break; // For Multiplication case '*': printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b); break; // For Division case '/': printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b); break; // If operator doesn't match any case constant default: printf("Error! please write a valid operator\n"); } }} |
Output:
Enter an operator (+, -, *, /), if want to exit press x: + Enter two first and second operand: 7 8 7.0 + 8.0 = 15.0 Enter an operator (+, -, *, /), if want to exit press x: - Enter two first and second operand: 8 9 8.0 - 9.0 = -1.0 Enter an operator (+, -, *, /), if want to exit press x: * Enter two first and second operand: 8 7 8.0 * 7.0 = 56.0 Enter an operator (+, -, *, /), if want to exit press x: / Enter two first and second operand: 8 3 8.0 / 3.0 = 2.7 Enter an operator (+, -, *, /), if want to exit press x: x
Using If-Else
Approach:
- We will do all the following steps inside a never-ending loop so that the calculator program keeps on working.
- Take input of operator and then operands.
- Check whether the user wants to quit the program if yes then quit it, for this, we can use a special character and tell the user about it, like here we used “x”.
- Using if-else statements we will check operators and do operations accordingly.
Example:
C
// C Program to Make a Simple Calculator// Using if else statement#include <stdio.h>#include <stdlib.h>int main(){ char ch; double a, b; while (1) { printf("Enter an operator (+, -, *, /), if want to exit press x: "); scanf(" %c", &ch;); // to exit if (ch == 'x') exit(0); printf("Enter two first and second operand: "); scanf("%lf %lf", &a;, &b;); // Using if else we will differentiate operations // based on different operator if (ch == '+') { // For Addition printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b); } else if (ch == '-') { // For Subtraction printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b); } else if (ch == '*') { // For Multiplication printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b); } else if (ch == '/') { // For Division printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b); } else { // If operator doesn't match any case constant printf("Error! please write a valid operator\n"); } }} |
Output:
Enter an operator (+, -, *, /), if want to exit press x: + Enter two first and second operand: 7 8 7.0 + 8.0 = 15.0 Enter an operator (+, -, *, /), if want to exit press x: - Enter two first and second operand: 8 9 8.0 - 9.0 = -1.0 Enter an operator (+, -, *, /), if want to exit press x: * Enter two first and second operand: 8 7 8.0 * 7.0 = 56.0 Enter an operator (+, -, *, /), if want to exit press x: / Enter two first and second operand: 8 3 8.0 / 3.0 = 2.7 Enter an operator (+, -, *, /), if want to exit press x: x





Please Login to comment...