0% found this document useful (0 votes)
14 views

Calc

C++ calculator

Uploaded by

02mxles
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Calc

C++ calculator

Uploaded by

02mxles
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

using namespace std;

double add(double num1, double num2) {

return num1 + num2;

double subtract(double num1, double num2) {

return num1 - num2;

double multiply(double num1, double num2) {

return num1 * num2;

double divide(double num1, double num2) {

if (num2 == 0) {

cout << "Error! Division by zero is not allowed.";

exit(1);

return num1 / num2;

int main() {

int choice;
double num1, num2;

cout << "Simple Calculator" << endl;

cout << "1. Addition" << endl;

cout << "2. Subtraction" << endl;

cout << "3. Multiplication" << endl;

cout << "4. Division" << endl;

while (true) {

cout << "Enter your choice (1-4): ";

cin >> choice;

if (choice >= 1 && choice <= 4) {

cout << "Enter first number: ";

cin >> num1;

cout << "Enter second number: ";

cin >> num2;

switch (choice) {

case 1:

cout << num1 << " + " << num2 << " = " << add(num1, num2) << endl;

break;

case 2:

cout << num1 << " - " << num2 << " = " << subtract(num1, num2) << endl;
break;

case 3:

cout << num1 << " * " << num2 << " = " << multiply(num1, num2) << endl;

break;

case 4:

cout << num1 << " / " << num2 << " = " << divide(num1, num2) << endl;

break;

} else {

cout << "Invalid input" << endl;

cout << "Do you want to do another calculation? (yes/no): ";

string response;

cin >> response;

if (response == "no") {

break;

return 0;

```

You might also like