c++ calculator program code example
Example 1: 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;
}
}
Example 2: c++ calculator program using switch case
#include<iostream>
using namespace std;
int calculator(int num1,int num2,int num3);
int main()
{
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;
}
}
Example 3: how to make a calculator in c++
#include <iostream>
using namespace std;
void greetings() {
cout << "welcome to the calculator made in c++ :D\n";
}
void instructions() {
cout << "Here is the operators you can use + - / *\n";
}
int main()
{
greetings();
instructions();
int num1, num2;
char op;
int result;
char again = 'Y';
while (again == 'y' || again == 'Y') {
cout << "\nEnter your first digit: ";
cin >> num1;
cout << "\nEnter your operator digit: ";
cin >> op;
cout << "\nEnter your second digit: ";
cin >> num2;
if (op == '+') {
result = num1 + num2;
}
else if (op == '-') {
result = num1 - num2;
}
else if (op == '*') {
result = num1 * num2;
}
else if (op == '/') {
result = num1 / num2;
}
else {
cout << "Invalid operator";
}
cout << "= " << result;
cout << "\nDo you want to restart the calculator? (Y or N)";
cin >> again;
}
system("pause>0");
return 0;
}
Example 4: how to build a calculator using c++
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
char op;
cout << "Enter a number:" << endl;
cin >> num1;
cout << "Enter another number:" << endl;
cin >> num2;
cout << "Enter a operator:" << endl;
cin >> op;
if(op == '+')
{
cout << "Result = " << num1 + num2 << endl;
}else if(op == '-'){
cout << "Result = " << num1 - num2 << endl;
}else if(op == '*'){
cout << "Result = " << num1 * num2 << endl;
}else if(op == '/'){
cout << "Result = " << num1 / num2 << endl;
}
}