Skip to content

Commit 1e78159

Browse files
authored
Merge pull request #251 from abhishekmail-prog/add-simple-calculator-cpp
Add SimpleCalculator.cpp in Playground/abhishekmail-prog
2 parents 6a25575 + b32f58c commit 1e78159

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
=====================================================
3+
Simple Calculator
4+
Author: Abhishek
5+
Language: C++
6+
Repository: Polyglot-Calculators
7+
8+
Description:
9+
A simple menu-driven calculator that performs:
10+
1. Addition
11+
2. Subtraction
12+
3. Multiplication
13+
4. Division
14+
15+
=====================================================
16+
*/
17+
18+
#include <iostream>
19+
using namespace std;
20+
21+
double add(double a, double b);
22+
double subtract(double a, double b);
23+
double multiply(double a, double b);
24+
double divide(double a, double b);
25+
26+
int main() {
27+
int choice;
28+
double num1, num2;
29+
30+
do {
31+
cout << "\n==== Simple Calculator ====\n";
32+
cout << "1. Add\n";
33+
cout << "2. Subtract\n";
34+
cout << "3. Multiply\n";
35+
cout << "4. Divide\n";
36+
cout << "5. Exit\n";
37+
cout << "Enter your choice: ";
38+
cin >> choice;
39+
40+
if (choice >= 1 && choice <= 4) {
41+
cout << "Enter two numbers: ";
42+
cin >> num1 >> num2;
43+
44+
switch (choice) {
45+
case 1:
46+
cout << "Result: " << add(num1, num2) << endl;
47+
break;
48+
case 2:
49+
cout << "Result: " << subtract(num1, num2) << endl;
50+
break;
51+
case 3:
52+
cout << "Result: " << multiply(num1, num2) << endl;
53+
break;
54+
case 4:
55+
if (num2 == 0) {
56+
cout << "Error: Division by zero not allowed.\n";
57+
} else {
58+
cout << "Result: " << divide(num1, num2) << endl;
59+
}
60+
break;
61+
}
62+
} else if (choice != 5) {
63+
cout << "Invalid choice. Try again.\n";
64+
}
65+
66+
} while (choice != 5);
67+
68+
cout << "Exiting calculator. Goodbye!\n";
69+
return 0;
70+
}
71+
72+
double add(double a, double b) {
73+
return a + b;
74+
}
75+
double subtract(double a, double b) {
76+
return a - b;
77+
}
78+
double multiply(double a, double b) {
79+
return a * b;
80+
}
81+
double divide(double a, double b) {
82+
return a / b;
83+
}
84+

0 commit comments

Comments
 (0)