Calculator Program in C++

In this post, we will develop a calculator program in C++, which asks two numbers and operators from the end-user and display the result. It will perform addition, subtraction, multiplication, and division operations based on the operator end-user entered.

// C++ Program to create a Simple Calculator
// Using switch-case statement
#include<iostream>
using namespace std;
int main()
{
  // declare variables
  char c;
  float num1, num2;

  // take input for operator
  cout << "Enter an Operator (+, -, *, /): ";
  cin >> c;

  // take numbers input
  cout << "Enter two Numbers:";
  cin >> num1 >> num2;

  switch(c)
  {
    case '+':
          cout << num1 << " + " << num2 << " = " << num1+num2 << endl;
          break;
    case '-':
          cout << num1 << " - " << num2 << " = " << num1-num2 << endl;
          break;
    case '*':
          cout << num1 << " * " << num2 << " = " << num1*num2 << endl;
          break;
    case '/':
          cout << num1 << " / " << num2 << " = " << num1/num2 << endl;
          break;
    default:
         cout << "Error! Invalid Operator" << endl;
  }

  return 0;
}

Output for the different test-case:-

Enter an Operator (+, -, *, /): +
Enter two Numbers:20
10
20 + 10 = 30

Enter an Operator (+, -, *, /): –
Enter two Numbers:20
10
20 – 10 = 10

Enter an Operator (+, -, *, /): *
Enter two Numbers:20
10
20 * 10 = 200

Enter an Operator (+, -, *, /): /
Enter two Numbers:20
10
20 / 10 = 2

Enter an Operator (+, -, *, /): %
Enter two Numbers:10
5
Error! Invalid Operator

When compiler encounters exit(0) then control came out from the program. This program will keep executing until the user does not choose the exit option. Here, the do-while loop is used to execute the same logic multiple times.

// Manu driven Calculator Program in C++
// using function
#include<iostream>
using namespace std;

// function declaration
double add(double, double);
double subtract(double, double);
double multiply(double, double);
double divide(double, double);
void display(double, double, double, char);

// main function
int main()
{
  // declare variables
  double num1, num2;
  int choice;
  char ch;
  
  do{
      // take inputs from end-user
      cout << "Enter two Numbers : ";
      cin >> num1 >> num2;
      
      // Menu to choose operation
      cout << "1. Addition" << endl;
      cout << "2. Subtraction" << endl;
      cout << "3. Multiplication" << endl;
      cout << "4. Division" << endl;
      cout << "5. Exit" << endl;
      
      // take input for operation
      cout << "Enter your choice : ";
      cin >> choice;
      
      switch(choice)
      {
        case 1:
          cout << "Result = " << add(num1, num2) << endl;
          break;
        case 2:
          cout << "Result = " << subtract(num1, num2) << endl;
            break;
        case 3:
          cout << "Result = " << multiply(num1, num2) << endl;
          break;
        case 4:
          cout << "Result = " << divide(num1, num2) << endl;
          break;
        case 5:
          cout << "Thank You." << endl;
          exit(0);
        default:
          cout << "Error! Invalid Operator" << endl;
      }

      cout << "Do you want to continue? Y/N :: " ;
      cin >> ch;
      cout << "-----------------------------" << endl;
  } while(ch == 'Y');

  cout << "Thank You." << endl;
  return 0;
}

// function to calculate Addition
double add(double n1, double n2)
{
    return n1 + n2;
}

// function to calculate Subtraction
double subtract(double n1, double n2)
{
    return n1 - n2;
}

// function to calculate Multiplication
double multiply(double n1, double n2)
{
    return n1 * n2;
}

// function to calculate Division
double divide(double n1, double n2)
{
    return n1 / n2;
}

Output for test-case-1:-

Enter two Numbers: 10 20
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 1
Result = 30
Do you want to continue? Y/N:: Y
—————————–
Enter two Numbers: 20 15
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 2
Result = 5
Do you want to continue? Y/N:: Y
—————————–
Enter two Numbers: 5 2
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice : 3
Result = 10
Do you want to continue? Y/N:: Y
—————————–
Enter two Numbers: 50 12
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 4
Result = 4.16667
Do you want to continue? Y/N:: Y
—————————–
Enter two Numbers: 5 2
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 6
Error! Invalid Operator
Do you want to continue? Y/N:: N
—————————–
Thank You.

Output for test-case-2:-

Enter two Numbers: 5 2
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 5
Thank You.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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