How to write a C Program to Create Simple Calculator using Switch case, Functions, and Else If Statement.
C Program to Create Simple Calculator Example 1
This calculator program in C helps the user to enter the Operator (+, -, *, or /) and two values. Using those two values and operand, it will perform Arithmetic Operations.
For this C calculator program example, we used the Switch case to check which operand is inserted by the user. Next, based on the Operand result will display.
/* C Program to Create Simple Calculator using Switch Case */ #include <stdio.h> int main() { char Operator; float num1, num2, result = 0; printf("\n Please Enter an Operator (+, -, *, /) : "); scanf("%c", &Operator); printf("\n Please Enter the Values for two Operands: num1 and num2 : "); scanf("%f%f", &num1, &num2); switch(Operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default: printf("\n You have enetered an Invalid Operator "); } printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); return 0; }
C Program to Create Simple Calculator Example 2
It is the same C program that we used in the first example. But this time, we are performing the arithmetic operation directly inside the Switch case blocks.
#include <stdio.h> int main() { char Operator; float num1, num2, result = 0; printf("\n Please Enter an Operator (+, -, *, /) : "); scanf("%c", &Operator); printf("\n Please Enter the Values for two Operands: num1 and num2 : "); scanf("%f%f", &num1, &num2); switch(Operator) { case '+': printf("\n The result of %.2f + %.2f = %.2f", num1, num2, num1 + num2); break; case '-': printf("\n The result of %.2f - %.2f = %.2f", num1, num2, num1 - num2); break; case '*': printf("\n The result of %.2f * %.2f = %.2f", num1, num2, num1 * num2); break; case '/': printf("\n The result of %.2f / %.2f = %.2f", num1, num2, num1 / num2); break; default: printf("\n You have enetered an Invalid Operator "); } return 0; }
Please Enter an Operator (+, -, *, /) : +
Please Enter the Values for two Operands: num1 and num2 : 125 179
The result of 125.00 + 179.00 = 304.00P
Program to Create Simple Calculator Example 3
In this example, we are using the Else If Statement to create a simple calculator in c.
#include <stdio.h> int main() { char Operator; float num1, num2, result = 0; printf("\n Please Enter an Operator (+, -, *, /) : "); scanf("%c", &Operator); printf("\n Please Enter the Values for two Operands: num1 and num2 : "); scanf("%f%f", &num1, &num2); if(Operator == '+') { printf("\n The result of %.2f + %.2f = %.2f", num1, num2, num1 + num2); } else if(Operator == '-') { printf("\n The result of %.2f - %.2f = %.2f", num1, num2, num1 - num2); } else if(Operator == '*') { printf("\n The result of %.2f * %.2f = %.2f", num1, num2, num1 * num2); } else if(Operator == '/') { printf("\n The result of %.2f / %.2f = %.2f", num1, num2, num1 / num2); } else { printf("\n You have enetered an Invalid Operator "); } return 0; }
Please Enter an Operator (+, -, *, /) : /
Please Enter the Values for two Operands: num1 and num2 : 1465 32
The result of 1465.00 / 32.00 = 45.78
Program to Create Simple Calculator Example 4
It is the same calculator program that we used in the first example. However, we separated the C Programming arithmetic operations logic using Functions.
#include <stdio.h> float add(float a, float b); float sub(float a, float b); float multi(float a, float b); float divi(float a, float b); int main() { char Operator; float num1, num2, result = 0; printf("\n Please Enter an Operator (+, -, *, /) : "); scanf("%c", &Operator); printf("\n Please Enter the Values for two Operands: num1 and num2 : "); scanf("%f%f", &num1, &num2); switch(Operator) { case '+': result = add(num1, num2); break; case '-': result = sub(num1, num2); break; case '*': result = multi(num1, num2); break; case '/': result = divi(num1, num2); break; default: printf("\n You have enetered an Invalid Operator "); } printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); return 0; } float add(float a, float b) { return a + b; } float sub(float a, float b) { return a - b; } float multi(float a, float b) { return a * b; } float divi(float a, float b) { return a / b; }
Please Enter an Operator (+, -, *, /) : -
Please Enter the Values for two Operands: num1 and num2 : 1462 2932.85
The result of 1462.00 - 2932.85 = -1470.85
Comments are closed.