
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Java Program to Make a Simple Calculator Using switch...case
- Golang Program to make a Simple Calculator using Switch Case
- Haskell Program to Make a Simple Calculator Using switch...case
- Java program to generate a calculator using the switch case
- C/C++ program to make a simple calculator?
- Switch case calculator in JavaScript
- How to write a simple calculator program using C language?
- Java program to find whether given character is vowel or consonant using switch case
- Create a simple calculator using Java Swing
- How to build a simple GUI calculator using tkinter in Python
- Menu Driven C++ Program for a Simple Calculator
- Simple GUI calculator using Tkinter in Python
- C program to find the areas of geometrical figures using switch case
- Creating a Simple Calculator using HTML, CSS, and JavaScript
- How to Add or Subtract Weeks to a Date in Excel?
