c++ calculator code code example

Example 1: how to make a calculator inc++

// ONLY 29 LINES OF CODE IN TOTAL
// For better calculator scroll down on this post
#include <iostream>
using namespace std;
int main() {
	cout << "Please enter the first number: ";
	double n1 = 0;
	cin >> n1;
	cout << "Please enter an operator (+, -, *, /): ";
	char op = '0';
	cin >> op;
	cout << "Please enter a second number: ";
	double n2 = 0;
	cin >> n2;
	double answer = 0;
	switch (op) {
	case '+': 
		answer = n1 + n2;
		break;
	case '-':
		answer = n1 - n2;
		break;
	case '*': 
		answer = n1 * n2;
		break;
	case '/': 
		answer = n1 / n2;
		break;
	}
	cout << "Thanks for using my calculator your answer is: " << answer;
  
  // this is the better calculator:
  
  #include <iostream>
#include <Windows.h>
using namespace std;
int main() {
	char jim = 'j';
	do {
		
		cout << "Please enter the first number: ";
		double n1 = 0;
		cin >> n1;
		cout << "Please enter an operator (+, -, *, /): ";
		char op = '0';
		cin >> op;
		cout << "Please enter a second number: ";
		double n2 = 0;
		cin >> n2;
		double answer = 0;
		switch (op) {
		case '+':
			answer = n1 + n2;
			break;
		case '-':
			answer = n1 - n2;
			break;
		case '*':
			answer = n1 * n2;
			break;
		case '/':
			answer = n1 / n2;
			break;
		}
		cout << "Thanks for using my calculator your answer is: " << answer << " do you want to run the program again? (y or n) ";
		cin >> jim;

	} while (jim = 'y' && jim != 'n');
	if (jim = 'n') {
		cout << "Thanks for using my calculator here is sum epic music";
		Beep(329, 300);
		Beep(493, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(783, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(329, 100);
		Beep(493, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(392, 250);
		Beep(440, 200);
		Beep(587, 300);

		Beep(349, 250);
		Beep(587, 500);

		Beep(329, 300);
		Beep(493, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(783, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(329, 100);
		Beep(493, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(392, 250);
		Beep(440, 200);
		Beep(587, 300);

		Beep(349, 250);
		Beep(587, 400);
	}
	return 0;
}

Example 2: c++ calculator

#include <iostream>
using namespace std;

int main() {
    char setOperator;
    float firstNum = 0.00, secondNum = 0.00;
    cout << "Enter your operater: +, -, *, /:  \n";
    cin >> setOperator;
    cout << "Enter your first calculation:  \n";
    cin >> firstNum;
    cout <<  "Enter your second calculation:  \n";
    cin >> secondNum;
    
    switch(setOperator) {
        case '+':
        cout << "The answer is: " <<firstNum + secondNum;
        break;
        
        case '-':
        cout << "The answer is:" << firstNum - secondNum;
        break;
        
        case '*':
        cout << "The answer is: " << firstNum * secondNum;
        break;
        
        case '/':
        cout << "The answer is: " << firstNum / secondNum;
        break;
    }
    return 0;
}

Example 3: c++ calculator script

#include <iostream>

using namespace std;

int main()
{
cout << "Welcome to the Calculator, write :cancel: to cancel the calculator\n";
 string cancel;

 while(cancel != "cancel")
 {
     int number1 = 0;
     int number2 = 0;
     string op;

     cout << "please enter your first number!\n";

     cin >> number1;

     cout << "please enter your second number!\n";

     cin >> number2; 

     cout << "please enter a operator!\n";
     cout << "( *, / , + , - )";

     cin >> op;
    
    if(op == "*")
    {
        number1 = number1 * number2;
        cout << number1; 
        number1 = 0;
        number2 = 0;
    }

    if(op == "/")
    {
        number1 = number1 / number2;
        cout << number1;
        number1 = 0;
        number2 = 0;
    }

    if(op == "+")
    {
        number1 = number1 + number2; 
        cout << number1;
        number1 = 0;
        number2 = 0;
    }

    if(op == "-")
    {
        number1 = number1 - number2;
        cout << number1; 
        number1 = 0; 
        number2 = 0;
    }
    
    cout << "Write cancel to cancel\n";
    cout << "write continue to resume with the calculator!\n";
    
    cin >> cancel;

 }
  
  
}
//powered by my coding server: https://discord.gg/GUgnYHJV

Example 4: c++ calculator program using switch case

#include<iostream>
using namespace std;
int calculator(int num1,int num2,int num3);
int main()
{
    //int num1,num2,num3,choice;
    int a,b,c,choice;
    cout<<"enter the first number"<<endl;
    cin>>a;
    cout<<"enter the second number"<<endl;
    cin>>b;
    cout<<"************************"<<endl;
    cout<<"****Make your choice****"<<endl;
    cout<<"1.Addition"<<endl;
    cout<<"2.Subtraction"<<endl;
    cout<<"3.Multiplication"<<endl;
    cout<<"4.Division"<<endl;
    cout<<"************************"<<endl;

    choice=calculator(a,b,c);
    return 0;
}
int calculator(int num1,int num2,int num3)
{

    int choice;
    cin>>choice;
    switch(choice)
    {

        case 1: cout<<"you have selected Addition"<<endl;
                num3=num1+num2;
                cout<<"Addition of two numbers is "<<num3<<endl;
                break;

        case 2: cout<<"you have selected Subtraction"<<endl;
                num3=num1-num2;
                cout<<"Subtraction of two numbers is "<<num3<<endl;
                break;

        case 3: cout<<"you have selected Multiplication"<<endl;
                num3=num1*num2;
                cout<<"Multiplication of two numbers is "<<num3<<endl;
                break;

        case 4: cout<<"You have selected Division"<<endl;
                num3=num1/num2;
                cout<<"Division of two numbers is "<<num3<<endl;
                break;

        default: cout<<"You have entered wrong choice"<<endl;
                    break;
    }
}

Tags:

Cpp Example