Home › Tutorials & Guides › Coding Tutorials › Calculator Program in C++

Calculator Program in C++

A calculator is a portable device or a software program that helps to perform simple arithmetical calculations in our daily lives such as addition, subtraction, division, multiplication, etc. Some of the scientific calculators are used to perform complex mathematical calculation more easily like square root, cube root, exponential operations, logarithm, trigonometric function and hyperbolic function, etc.

In this article, we will create calculator program using functions and do-while loop.

Calculator Program in C++ Using Functions

Lets’ create a calculator program using the functions and Switch statement.

#include<iostream>    
#include<cmath>   
using namespace std;
void division();  
void multi();  
void add();  
void sub();  
void sqr();  
void pow();
void srt();  
void crt();
int main()  
{  
int opr;  
// display different operation of the calculator  
do  
{  
cout << "Select any operation from the C++ Calculator"  
     "\n1 = Division"  
     "\n2 = Multiplication"  
     "\n3 = Addition"  
     "\n4 = Subtraction"  
     "\n5 = Square"  
     "\n6 = Power"
     "\n7 = Square Root"  
     "\n8 = Cube Root"
     "\n9 = Exit"  
     "\n \n Make a choice: ";  
     cin >> opr;  
  
   switch (opr)  
     {  
     case 1:  
    division();  // call add() function to find the Addition  
    break;  
    case 2:  
    multi();   // call sub() function to find the subtraction  
    break;  
    case 3:  
    add(); // call multi() function to find the multiplication  
    break;  
    case 4:  
    sub(); // call division() function to find the division  
    break;  
    case 5:  
    sqr(); // call sqr() function to find the square of a number  
    break;  
    case 6:
    pow(); // call pow() function to find the nth power of a number
    break;
    case 7:  
    srt(); // call srt() function to find the Square Root of the given number  
    break;  
    case 8:
    crt(); // call crt() function to find the Cube Root of the given number
    break;
    case 9:  
    exit(0);   // terminate the program  
    break;  
    default:  
    cout <<"Something is wrong..!!";  
    break;  
    }  
    cout <<" \n------------------------------\n";  
    }while(opr != 9);   
  
  return 0;
    }  

void multi()  
{  
float num1, num2, mul;  
cout <<" \n Enter the First number = ";  
cin >> num1;  
cout << "\n Enter the Second number = ";  
cin >> num2;  
mul = num1 * num2;  
cout <<"\n Multiplication of two numbers = " << mul;  
}  
void division()  
{  
float num1, num2, div = 0;  
cout <<" \n Enter the First number = ";  
cin >> num1;  
cout << "\n Enter the Second number = ";  
cin >> num2;  
while ( num2 == 0)  
     {  
     cout << "\n Divisor can not be zero"  
         "\n Please enter the divisor once again: ";  
         cin >> num2;  
         }  
div = num1 / num2;  
cout <<"\n Division of two numbers = " << div;  
}  
void add()  
{  
int n;
float sum = 0, i, number;  
cout <<"How many numbers you want to add: ";  
cin >> n;  
cout << "Please enter the number one by one: \n";  
for (i = 1; i <= n; i++)  
{  
cin >> number;  
sum = sum + number;  
}  
cout << "\n Sum of the numbers = "<< sum;  
}  
void sub()  
{  
float num1, num2, z;  
cout <<" \n Enter the First number = ";  
cin >> num1;  
cout << "\n Enter the Second number = ";  
cin >> num2;  
z = num1 - num2;  
cout <<"\n Subtraction of the number = " << z;  
}  
void sqr()  
{  
float num1, sq;  
cout <<" \n Enter a number to find the Square: ";  
cin >> num1;  
sq = num1 * num1;  
cout <<" \n Square of " << num1<< " is : "<< sq;  
}  
void pow()  
{  
float base, exp, result;  
cout <<" \n Enter a base to find the nth power: ";  
     cin >> base; 
     cout <<" \n Enter a exponent: ";
     cin >> exp;
	 result = pow(base, exp);
     cout <<" \n " << base << " ^ " << exp<< " = "<< result;   
}  
void srt()  
{  
float num1, q;  
cout << "\n Enter the number to find the Square Root: ";  
cin >> num1;  
q = sqrt(num1);  
cout <<" \n Square Root of " << num1<< " is : "<< q;  
}  
void crt()  
{  
float num1, q;  
cout << "\n Enter the number to find the Cube Root: ";  
cin >> num1;  
q = cbrt(num1);  
cout <<" \n Cube Root of " << num1<< " is : "<< q;  
}  

Output:

Calculator Program in C++ Using do-while Loop

Calculator Program using the do while loop and Switch Statement.

#include<iostream>  
#include <cmath>
using namespace std;
  
int main()  
{  
int opr;  
float num1, num2, x;  
int n;
float sum, i, number;  
float base, exp, result;
// display different operation of the calculator  
do  
{  
cout << "Select an operation to perform a simple calculation in C++ Calculator"  
     "\n1 = Addition"  
     "\n2 = Subtraction"  
     "\n3 = Multiplication"  
     "\n4 = Division"  
     "\n5 = Square"  
     "\n6 = Power"
     "\n7 = Square Root"  
     "\n8 = Cube Root"
     "\n9 = Exit"  
     "\n \n Make a choice: ";  
     cin >> opr;  
   switch (opr)  
     {  
     // for addition operation in calculator  
     case 1:  
     
     cout <<"How many numbers you want to add: ";  
     cin >> n;  
     cout << "Please enter the number one by one: \n";  
     for (i = 1; i <= n; i++)  
     {  
      cin >> number;  
      sum = sum + number;  
      }  
     cout << "\n Sum of the numbers = "<< sum; 
     sum = 0;
     break;  
     // for subtraction operation in calculator  
     case 2:  
     cout << "You have selected the Subtraction Operation.";  
     cout << "\n Please enter the two number: \n";  
     cin >> num1 >> num2;  
     x = num1 - num2;  
     cout << "Subtraction of two number = " << x;  
     break;  
     // for multiplication operation in calculator  
     case 3:  
     cout << "You have selected the Multiplication Operation.";  
     cout << "\n Please enter the two number: \n";  
     cin >> num1 >> num2;  
     x = num1 * num2;  
     cout << "Product of two number = " << x;  
     break;  
     // for division operation in calculator  
     case 4:  
     cout << "You have selected the Division Operation.";  
     cout << "\n Please enter the two number; \n";  
     cin >> num1 >> num2;  
     // while loop checks for divisor whether it is zero  
     while ( num2 == 0)  
     {  
     cout << "\n Divisor cannot be zero"  
         "\n Please enter the divisor once again: ";  
         cin >> num2;  
         }  
     x = num1 / num2;  
     cout << "\n Quotient = " << x;  
     break;  
     // to square a number in calculator  
     case 5:  
     cout << "You have selected the Square Operation.";  
      cout << "\n Please enter any number: \n";  
     cin >> num1;  
     x = num1 * num1;  
     cout << "Square is = " << x;  
     break;  
     // to find nth power of a number in calculator 
     case 6:
     cout <<" \n Enter a base to find the nth power: ";  
     cin >> base; 
     cout <<" \n Enter a exponent: ";
     cin >> exp;
	 result = pow(base, exp);
     cout <<" \n " << base << " ^ " << exp<< " = "<< result;  
     break;
     // to square root a number in calculator  
     case 7:
     cout << "\n Enter the number to find the Square Root: ";  
     cin >> num1;  
     x = sqrt(num1);  
     cout <<" \n Square Root of " << num1<< " is : "<< x;   
     break;
     // to cube root a number in calculator  
     case 8:
     cout << "\n Enter the number to find the Cube Root: ";  
     cin >> num1;  
     x = cbrt(num1);  
     cout <<" \n Cube Root of " << num1<< " is : "<< x;     
     break;
     case 9: exit(0);  // terminate the program  
     break;  
     default: cout << "\n Something went wrong..!!";  
     break;  
     }  
     cout << "\n----------------------------------------- \n";  
     } while(opr != 9);  
  
     return 0;
     }  

Output:

Note: If you interested lo learn C++ through web, You can click here

Conclusion:

Hi guys, I can hope that you can know that how to write a Calculator Program in C++. If you like this post as well as know something new so share this article in your social media accounts. If you have any doubt related to this post then you ask in comment section.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *