C++ Program to Make Simple Calculator to Add, Subtract, Multiply, Divide and Taking Power

by - June 11, 2019

Simple Calculator in C++

Calculator

An electronic calculator is typically a portable electronic device used to perform calculations, ranging from basic arithmetic to complex mathematics. Calculators are widely used device nowadays. Calculators are used to everyone in daily life. A simple calculator can be made using a C++ program which can perform addition, subtraction, multiplication division and power Operation. It takes two operands entered by the user. The switch and break statements are used to create a calculator.

Logic to make Simple Calculator Program in C++

  1. Input numbers along with operator
  2. Use switch statement to check for operator and execute respective case.
  3. Store Result in a variable and display it.

Write a program to make simple calculator in C++ which add, subtract, multiply, divide or take power.


// C++ Program to make simple calculator
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
                int a,b,sum,i,sum1,P;
                char op;
                cout << "Calculator (+,-,*,/,^)"<<endl;
                cout<<"Enter Expression: ";
                cin>>a>>op>>b;

                switch(op) {
                                case'+':
                                                sum=a+b;
                                                break;
                                case'-':
                                                sum=a-b;
                                                break;
                                case'*':
                                                sum=a*b;
                                                break;
                                case'/':
                                                sum=a/b;
                                                break;
                                case'^':
                                                P=a;
                                                for(i=1; i<b; i++) {

                                                                sum=(a*P);
                                                                P=P*a;
                                                }
                                                break;
                                default:
                                                break;
                }
                cout<<"Answer is: "<<sum<<endl;
                _getch();
}


Calculator (+,-,*,/,^)
Enter Expression: 2*3
Answer is: 6
Calculator (+,-,*,/,^)
Enter Expression: 2^3
Answer is: 8
Calculator (+,-,*,/,^)
Enter Expression: 4/2
Answer is: 2

You May Also Like

0 comments