Creating a basic calculator is certainly fun for beginners in programming. A basic calculator must perform addition, subtraction, multiplication, and division. There are various ways in programming to create a calculator.
In this article, we will use a switch case to write our C and C++ programs to perform basic arithmetic operations. But, let us first understand briefly what a switch case is.
What is a Switch Case in Programming?
A switch case is a control statement in programming that executes a single code block among multiple alternatives based on the value of the stated expression or variable. It is analogous to the 'if...else...if" ladder. However, the syntax of the switch case statement is much easier to understand and write.
Initially, the switch case takes an expression or variable. If it is an expression, it evaluates and obtains its value. If it is a variable, it directly takes its assigned value. Later, it compares this value with a set of case values mentioned in a switch case. If any value from the set matches, it executes that block of code.
Syntax
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
...
default:
// code block
break;
}
Now, you have an idea about the switch case statement. You can use it to create a basic calculator.
In our blog post, we will discuss creating a basic calculator using C and C++ . It will be capable of performing four basic arithmetic operations - addition, subtraction, multiplication, and division. The calculator will first ask users to choose the operation and then enter two numbers. It will display the result immediately.
So, are you ready? Here we go!
C Program to Perform Arithmetic Operations Using a Switch Case
#include <stdio.h>
int main()
{
char operator;
double num1, num2;
//ask user to enter the operator
printf("Select the operator (+, -, *, /): ");
scanf("%c", &operator);
//ask user to enter two numbers
printf("Enter two numbers (eg. 12 3): ");
scanf("%lf %lf",&num1, &num2);
//start switch case
switch(operator)
{ //if operator is addition
case '+':
printf("%.1lf + %.1lf = %.1lf",num1, num2, num1+num2);
break;
// if operator is substraction
case '-':
printf("%.1lf - %.1lf = %.1lf",num1, num2, num1-num2);
break;
//if operator is multiplication
case '*':
printf("%.1lf * %.1lf = %.1lf",num1, num2, num1*num2);
break;
//if operator is division
case '/':
printf("%.1lf / %.1lf = %.1lf",num1, num2, num1/num2);
break;
// if user enter a wrong operaotr
default:
printf("Please select the correct operator");
}
return 0;
}
Output
Select the operator (+, -, *, /): *
Enter two numbers (eg. 12 3): 4 5
4.0 * 5.0 = 20.0
C++ Program to Perform Arithmetic Operations Using a Switch Case
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float num_1,num_2;
int operation;
cout<<"What Arithmetic Operation do you want to perform:\n";
cout<<"Press 1 for Addition \n" ;
cout<<"Press 2 for Subtraction\n";
cout<<"Press 3 for Multiplication\n";
cout<<"Press 4 for Division\n";
cin>>operation;
cout<<"Now Enter Two Numbers\n";
cin>>num_1>>num_2;
switch (operation)
{
case 1:
cout<<"The Addition result is: "<<num_1+num_2;
break;
case 2:
cout<<"The Subtraction result is: "<<num_1-num_2;
break;
case 3:
cout<<"The Multiplication result is: "<<num_1*num_2;
break;
case 4:
cout<<"The Division result is: "<<num_1/num_2;
break;
}
getch();
}
Sample Output:
What Arithmetic Operation do you want to perform:
Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
4
Now Enter Two Numbers
100
30
The Division result is: 3.33333
C++ Program for Arithmetic Operations Using Switch Case and Function
The above example is not modular. We have implemented all the logic in the main program. We can also implement the same using a C++ function .
You would ask why to use the function. The answer is simple. Functions make the code modular and easy to read. For this particular example, the function implementation won't change the program's legibility much.
Now, let's write a menu-driven program to perform arithmetic operations in C++ using function and switch case.
#include <iostream>
using namespace std;
// function to perfrom C++ operation
void arithmetic(float num_1, float num_2)
{
int operation;
cout<<"What Arithmetic Operation do you want to perform:\n";
cout<<"Press 1 for Addition \n" ;
cout<<"Press 2 for Subtraction\n";
cout<<"Press 3 for Multiplication\n";
cout<<"Press 4 for Division\n"; cin>>operation;
switch (operation)
{
case 1:
cout<<"The Addition result is: "<<num_1+num_2;
break;
case 2:
cout<<"The Subtraction result is: "<<num_1-num_2;
break;
case 3:
cout<<"The Multiplication result is: "<<num_1*num_2;
break;
case 4:
cout<<"The Division result is: "<<num_1/num_2;
break;
}
}
int main() {
float num_1, num_2;
cout<<"Now Enter Two Numbers\n"; cin>>num_1>>num_2;
// call the function
arithmetic(num_1, num_2);
return 0;
}
Output
Now Enter Two Numbers
20 40
What Arithmetic Operation do you want to perform:
Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division
1
The Addition result is: 60
Conclusion
That sums up this demonstration to perform arithmetic operations using a switch case in C++. With the programs mentioned above, we have developed a basic calculator that can perform addition, subtraction, multiplication, and division.
In this article, we have covered two different ways to implement a C++ program for arithmetic operation using a switch case. Note that this is only one way of writing this program. You can also try to write it as you find best. The more options you'll explore, the better you will learn.
All the best!
People are also reading:
Leave a Comment on this Post