Professional Documents
Culture Documents
Calculator Project by Switch Statement
Calculator Project by Switch Statement
#include <cmath>
int main() {
while (true) {
cout << "Select operation:" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "5. Square Root" << endl;
cout << "6. Cube Root" << endl;
cout << "7. Natural Logarithm (ln)" << endl;
cout << "8. Decimal Logarithm (log10)" << endl;
cout << "9. Sine (sin)" << endl;
cout << "10. Cosine (cos)" << endl;
cout << "11. Tangent (tan)" << endl;
cout << "12. Inverse Sine (asin)" << endl;
cout << "13. Inverse Cosine (acos)" << endl;
cout << "14. Inverse Tangent (atan)" << endl;
cout << "15. Absolute Value (abs)" << endl;
cout << "16. Exponent (exp)" << endl;
cout << "17 sinh" << endl;
cout << "18 cosh" << endl;
int choice;
cin >> choice;
switch(choice) {
case 1: { // Addition
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
break;
}
case 2: { // Subtraction
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
break;
}
case 3: { // Multiplication
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
break;
}
case 4: { // Division
double num1, num2;
cout << "Enter numerator: ";
cin >> num1;
cout << "Enter denominator: ";
cin >> num2;
if (num2 == 0) {
cout << "Error! Cannot divide by zero." << endl;
} else {
cout << num1 << " / " << num2 << " = " << num1/num2 << endl;
}
break;
}
case 5: { // Square Root
double num;
cout << "Enter a number to find its square root: ";
cin >> num;
if (num < 0) {
cout << "The square root of negative numbers is not defined!" << endl;
} else {
cout << "Square root of " << num << " = " << sqrt(num) << endl;
}
break;
}
case 6: { // Cube Root
double num;
cout << "Enter a number to find its cube root: ";
cin >> num;
cout << "Cube root of " << num << " = " << cbrt(num) << endl;
break;
}
case 7: { // Natural Logarithm (ln)
double num;
cout << "Enter a number to find its natural logarithm: ";
cin >> num;
if (num <= 0) {
cout << "The natural logarithm of non-positive numbers is not
defined!" << endl;
} else {
cout << "Natural logarithm of " << num << " = " << log(num) << endl;
}
break;
}
case 8: { // Decimal Logarithm (log10)
double num;
cout << "Enter a number to find its decimal logarithm: ";
cin >> num;
if (num <= 0) {
cout << "The decimal logarithm of non-positive numbers is not
defined!" << endl;
} else {
cout << "Decimal logarithm of " << num << " = " << log10(num) <<
endl;
}
break;
}
case 9: { // Sine (sin)
double num;
cout << "Enter an angle in radians to find its sine value: ";
cin >> num;
cout << "Sine of " << num << " = " << sin(num) << endl;
break;
}
case 10: { // Cosine (cos)
double num;
cout << "Enter an angle in radians to find its cosine value: ";
cin >> num;
cout << "Cosine of " << num << " = " << cos(num) << endl;
break;
}
case 11: { // Tangent (tan)
double num;
cout << "Enter an angle in radians to find its tangent value: ";
cin >> num;
cout << "Tangent of " << num << " = " << tan(num) << endl;
break;
}
case 12: { // Inverse Sine (asin)
double num;
cout << "Enter a number between -1 and 1 to find its inverse sine value:
";
cin >> num;
char option;
cout << "Do you want to continue with another operation? (y/n) ";
cin >> option;
return 0;
}