0% found this document useful (0 votes)
53 views9 pages

Calculator Project by Switch Statement

This C++ program provides a menu-driven calculator that allows the user to select and perform various mathematical operations on numbers. The operations include basic arithmetic, trigonometric, logarithmic, exponential and hyperbolic functions. The user is prompted to select an operation, enter the needed numbers, and the result is displayed. The user can then choose to continue or quit the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views9 pages

Calculator Project by Switch Statement

This C++ program provides a menu-driven calculator that allows the user to select and perform various mathematical operations on numbers. The operations include basic arithmetic, trigonometric, logarithmic, exponential and hyperbolic functions. The user is prompted to select an operation, enter the needed numbers, and the result is displayed. The user can then choose to continue or quit the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include <iostream>

#include <cmath>

using namespace std;

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;

if (num < -1 || num > 1) {


cout << "Error! Input must be between -1 and 1." << endl;
} else {
cout << "Inverse sine of " << num << " = " << asin(num) << endl;
}
break;
}
case 13: { // Inverse Cosine (acos)
double num;
cout << "Enter a number between -1 and 1 to find its inverse cosine
value: ";
cin >> num;

if (num < -1 || num > 1) {


cout << "Error! Input must be between -1 and 1." << endl;
} else {
cout << "Inverse cosine of " << num << " = " << acos(num) << endl;
}
break;
}
case 14: { // Inverse Tangent (atan)
double num;
cout << "Enter a number to find its inverse tangent value: ";
cin >> num;
cout << "Inverse tangent of " << num << " = " << atan(num) << endl;
break;
}
case 15: { // Absolute Value (abs)
double num;
cout << "Enter a number to find its absolute value: ";
cin >> num;
cout << "Absolute value of " << num << " = " << abs(num) << endl;
break;
}
case 16: { // Exponent (exp)
double num;
cout << "Enter a number to find its exponential value (e^x): ";
cin >> num;
cout << "Exponential value of " << num << " = " << exp(num) << endl;
break;
}
case 17: { // sinh
double num;
cout << "Enter a number to find its hyperbolic sine value (sinh): ";
cin >> num;
cout << "Hyperbolic sine of " << num << " = " << sinh(num) << endl;
break;
}
case 18:{//cosh
double num;
cout << "Enter a number to find its hyperbolic cosine value
(cosh): ";
cin >> num;
cout <<"Hyperbolic cosine of " << num << " = " << cosh(num) <<
endl;
break;
}
default:
cout << "Error! Invalid input." << endl;
break;
}

char option;
cout << "Do you want to continue with another operation? (y/n) ";
cin >> option;

if (option == 'n' || option == 'N') {


break;
}
}

return 0;
}

You might also like