Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Calculator
3.
Implementation in C++
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Calculator Program in C++

Introduction 

In this article, we are going to discuss the calculator program in C++. We will implement some functions of the calculator like addition, subtraction, multiplication, and more in c++ language.  

Calculator

A calculator is a portable instrument that aids us in doing simple mathematical calculations such as addition, subtraction, division, multiplication, and so on in our daily life. Scientific calculators simplify difficult calculations such as square roots, functions, exponential operations, logarithms, trigonometric functions, and hyperbolic functions, among others. We'll make a calculator program in C++. 

We are going to implement various functions of the calculator such as 

  1. Addition 
  2. Subtraction 
  3. Multiplication
  4. Division 
  5. Square root 
  6. Exit Function 

Implementation in C++

// C++ program for the calculator
#include <bits/stdc++.h>
using namespace std;

// utility function to add the numbers in calculator
void addition()
{
    // declaring the sum variable to hold the total sum;
    int sum = 0;
    int n;
    // taking input from the users how many numbers want to be added in variable n
    cout << "Enter the numbers you want to add: ";
    cin >> n;
    cout << "Input the numbers one by one: " << endl;
    // running the loop for n numbers
    for (int i = 1; i <= n; i++)
    {
        int input;
        cin >> input;
        sum = sum + input;
    }
    cout << "Addition of the numbers is: " << sum << endl;
}

// utility function to multiply in calculator
void subtraction()
{
    int num1, num2;
    int result;
    cout << " \n Input First Number = ";
    cin >> num1;
    cout << "\n Input second Number  = ";
    cin >> num2;
    result = num1 - num2;
    cout << "\n Subtraction of the number = " << result;
}

// utility function to multiply in calculator
void multiply()
{
    int num1, num2, result;
    cout << " \n Input First Number  = ";
    cin >> num1;
    cout << "\n Input second Number  = ";
    cin >> num2;
    result = num1 * num2;
    cout << "\n Multiplication of two numbers = " << result << endl;
}

// utitlity function to perform the division in calculator
void division()
{
    int num1, num2, result;
    cout << endl
         << "Input First Number  = ";
    cin >> num1;
    cout << endl
         << "Input second Number  = ";
    cin >> num2;
    while (num2 == 0)
    {
        cout << "\n Divisor cannot be zero" << endl;
        cout << "Input divisor again " << endl;
        cin >> num2;
    }
    result = num1 / num2;
    cout << "\n Division of two numbers = " << result << endl;
}

// utility  function to find the square of the number in calculator
void square()
{
    int number;
    cout << " \n Input the number ";
    cin >> number;
    cout << " \n Square of " << number << " is : " << number * number << endl;
}

// utility function to find the square root of the number in calculator
void sqrt()
{
    double result;
    int number;
    cout << "\n Input the number";
    cin >> number;
    result = sqrt(number);
    cout << " \n Square Root of " << number << " is : " << result << endl;
}

// main function start here
int main()
{
    int input = 0;
    while(input!=7){
        cout << "Select the operation" << endl;
        cout << "Press 1 for addition" << endl;
        cout << "Press 2 for subtraction" << endl;
        cout << "Press 3 for mutiply" << endl;
        cout << "Press 4 for division" << endl;
        cout << "Press 5 for square" << endl;
        cout << "Press 6 for square root" << endl;
        cout << "Press 7 for exit" << endl;
        cin >> input;
        switch(input){
            case 1: addition(); break;
            case 2: subtraction(); break;
            case 3: multiply(); break;
            case 4: division(); break;
            case 5: square(); break;
            case 6: sqrt(); break;
            case 7: break;
        }
     }
}
You can also try this code with Online C++ Compiler
Run Code

 

Outputs:

Addition Function:

Select the operation
Press 1 for addition
Press 2 for subtraction
Press 3 for mutiply
Press 4 for division
Press 5 for square
Press 6 for square root
Press 7 for exit
1
Enter the numbers you want to add: 4
Input the numbers one by one: 
1 2 3 4 
Addition of the numbers is: 10

 

Subtraction Function:

Select the operation
Press 1 for addition
Press 2 for subtraction       
Press 3 for mutiply
Press 4 for division
Press 5 for square
Press 6 for square root
Press 7 for exit
2
 
Input First Number = 4

Input second Number  = 5

Subtraction of the number = -1

 

Multiplication Function:

Press 1 for addition
Press 2 for subtraction
Press 3 for mutiply
Press 4 for division
Press 5 for square
Press 6 for square root
Press 7 for exit
3
 
Input First Number  = 5

Input second Number  = 15

Multiplication of two numbers = 75

 

Division Function:

Select the operation
Press 1 for addition
Press 2 for subtraction
Press 3 for mutiply
Press 4 for division
Press 5 for square
Press 6 for square root
Press 7 for exit
4

Input First Number  = 25

Input second Number  = 5

Division of two numbers = 5

 

Square function:

Select the operation
Press 1 for addition
Press 2 for subtraction
Press 3 for multiply
Press 4 for division
Press 5 for square
Press 6 for square root
Press 7 for exit
5
 
Input the number 25
 
Square of 25 is : 625

 

Square root Function:

Select the operation
Press 1 for addition
Press 2 for subtraction
Press 3 for multiply
Press 4 for division
Press 5 for square
Press 6 for square root
Press 7 for exit
6

Input the number 25
 
Square Root of 25 is : 5

 

Try and compile by yourself with the help of online C++ Compiler for better understanding.

Frequently Asked Questions

  1. What are the data types present in c++? 
     The following are the four data types in C++ 
    → Datatype in its most basic form (basic datatype). For instance, char, short, int, float, long, double, bool, and so on.
    → A data type that has been derived. Arrays, pointers, and other similar concepts are examples.
    → enum is an example of enumeration.
    → Types of data that are specified by the user. Structure, class, and so on are examples.
     
  2. What is the meaning of operator overloading in c++?   
    To conduct operations on user-defined data types, operator overloading is a critical component. We may change the default meaning of operators like +, -, *, /, =, and others by using operator overloading.
     
  3. What are virtual functions in c++?  
    A virtual function is a member function of a base class that is redefined in a derived class. A virtual function is declared using the virtual keyword. When a function is made virtual, C++ determines which function should be performed at runtime based on the kind of object referenced by the base class pointer. 
     
  4. What are inline functions in c++?  
    If a function is inline, the compiler stores a copy of the function's code at each location where it is called during compile time. One of the most significant advantages of utilizing an inline function is that it avoids the cost of invoking a regular function.

Conclusion

In this article, we have done the code implementation of various functions present in the calculator such as addition, subtraction, multiplication, division, finding the square of a number, finding the square root of the number in c++ Language. We hope you understand the code implementation properly. Now you can do more similar questions. 

Recommended Readings:

If you are a beginner, interested in Coding, and want to learn DSA, you can look for our guided path for DSA, which is free! 

Thank you for reading. 

Until then, Keep Learning and Keep Coding.

Live masterclass