
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case
Let us see a program to create a simple calculator in C++, with Add, Subtract, Multiply and Divide operations.
Example
#include <iostream> using namespace std; void calculator(int a, int b, char op) { switch (op) { case '+': { cout<<"Sum of "<<a<<" and "<<b<<" is "<<a+b<<endl; break; } case '-': { cout<<"Difference of "<<a<<" and "<<b<<" is "<<a-b<<endl; break; } case '*': { cout<<"Product of "<<a<<" and "<<b<<" is "<<a*b<<endl; break; } case '/': { cout<<"Division of "<<a<<" and "<<b<<" is "<<a/b<<endl; break; } default: cout<<"Invalid Input"<<endl; } } int main() { calculator(5,4,'+'); calculator(10,3,'-'); calculator(3,2,'*'); calculator(20,5,'/'); calculator(5,2,'?'); return 0; }
Output
Sum of 5 and 4 is 9 Difference of 10 and 3 is 7 Product of 3 and 2 is 6 Division of 20 and 5 is 4 Invalid Input
In the above program, a function calculator is used to add, subtract, multiply and divide two numbers. This is done using a switch case statement. The function takes 3 parameters i.e. two numbers on which the operation is to be performed and what operation is to be performed. This is shown as follows −
void calculator(int a, int b, char op)
There are 4 cases in the switch case statement and one default case. The first case is used when addition is to be performed. The two numbers are added and their sum is displayed. This is shown using the following code snippet.
case '+': { cout<<"Sum of "<<a<<" and "<<b<<" is "<<a+b<<endl; break; }
The second case is used when subtraction is to be performed. The two numbers are subtracted and their difference is displayed. This is shown using the following code snippet.
case '-': { cout<<"Difference of "<<a<<" and "<<b<<" is "<<a-b<<endl; break; }
The third case is used when multiplication is to be performed. The two numbers are multiplied and their product is displayed. This is shown using the following code snippet.
case '*': { cout<<"Product of "<<a<<" and "<<b<<" is "<<a*b<<endl; break; }
The fourth case is used when division is to be performed. The two numbers are divided and their division is displayed. This is shown using the following code snippet.
case '/': { cout<<"Division of "<<a<<" and "<<b<<" is "<<a/b<<endl; break; }
The default case is used for invalid operator provided.This is shown using the following code snippet.
default: cout<<"Invalid Input"<<endl;
The function calculator() is called from main() for different operations and using different operands. This is demonstrated by the following code snippet.
calculator(5,4,'+'); calculator(10,3,'-'); calculator(3,2,'*'); calculator(20,5,'/'); calculator(5,2,'?');
- Related Questions & Answers
- Java Program to Make a Simple Calculator Using switch...case
- C/C++ program to make a simple calculator?
- Java program to generate a calculator using the switch case
- Switch case calculator in JavaScript
- How to write a simple calculator program using C language?
- Menu Driven C++ Program for a Simple Calculator
- Create a simple calculator using Java Swing
- Write a C program of library management system using switch case
- Java program to find whether given character is vowel or consonant using switch case
- C program to find the areas of geometrical figures using switch case
- How to build a simple GUI calculator using tkinter in Python
- Simple GUI calculator using Tkinter in Python
- Using range in switch case in C/C++
- Creating a Simple Calculator using HTML, CSS, and JavaScript
- Basic calculator program using C#