calculator with c++ code example
Example 1: 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 2: calculator with c++
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << 1 << endl;
cout << 2 << endl;
cout << 3 << endl;
cout << 4 << endl;
cout << "Choice A Number: ";
cin >> choice;
if (choice >= 1 && choice <= 4)
{
int a, b;
cout << "Enter Num One: ";
cin >> a;
cout << "Enter Num Two: ";
cin >> b;
if (choice == 1)
cout << a << "+" << b << "=" << a + b << endl;
if (choice == 2)
cout << a << "-" << b << "=" << a - b << endl;
if (choice == 3)
cout << a << "*" << b << "=" << a * b << endl;
}
else
{
cout << "Wrong Choice" << endl;
}
}
Example 3: 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;
}
}