34+324=358Operator−‘-’=>3874-324=3550Operator−‘*’=>76*24=1824O"/> 34+324=358Operator−‘-’=>3874-324=3550Operator−‘*’=>76*24=1824O">

Home >Backend Development >C++ >Write a simple calculator C/C++ program

Write a simple calculator C/C++ program

王林
王林forward
2023-09-02 22:49:12977browse

Write a simple calculator C/C++ program

Simple calculator is a calculator that performs some basic operations, such as " ", "-", "*", "/". The calculator can perform basic operations quickly. We will use a switch statement to make a calculator.

Example

Operator − ‘+’ => 34 + 324 = 358
Operator − ‘-’ => 3874 - 324 = 3550
Operator − ‘*’ => 76 * 24 = 1824
Operator − ‘/’ => 645/5 = 129

Sample code

#include <iostream<
using namespace std;
int main() {
   char op = &#39;+&#39;;
   float a = 63, b= 7;
   // cout << "Enter operator either + or - or * or /: ";
   // cin >> op;
   cout<<"The operator is "<<op<<endl;
   // cout << "Enter two operands: ";
   // cin>> a >> b;
   cout<<"a = "<<a<<" b = "<<b<<endl;
   switch(op) {
      case &#39;+&#39;:
         cout <<"Sum of"<<a<<"and"<<b<<"is"<< a+b;
      break;
      case &#39;-&#39;:
         cout <<"Difference of"<<a<<"and"<<b<<"is"<<a-b;
      break;
      case &#39;*&#39;:
         cout <<"Product of"<<a<<"and"<<b<<"is"<<a*b;
      break;
      case &#39;/&#39;:
         cout <<"Division of"<<a<<"and"<<b<<"is"<<a/b;
      break;
      default:
         // If the operator is other than +, -, * or /, error message is shown
         cout << "Error! operator is not correct";
      break;
   }
   return 0;
}

Output

The operator is +
a = 63 b = 7
Sum of63 and 7 is 70

The above is the detailed content of Write a simple calculator C/C++ program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete