Calculator Project by If Statement
Calculator Project by If 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;
if (choice == 1) { // Addition
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
}
else if (choice == 2) { // Subtraction
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
}
else if (choice == 3) { // Multiplication
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
}
else if (choice == 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;
}
}
else if (choice == 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;
}
}
else if (choice == 6) { // Cube Root
double num;
cout << "Enter a number to find its cube root: ";
cin >> num;
cout << "Cube root of " << num << " = " << cbrt(num) << endl;
}
else if (choice == 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;
}
}
else if (choice == 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;
}
}
else if (choice == 9) { // Sine
double angle , result;
char unit, choice;
cout << "Enter an angle: ";
cin >> angle;
cout << "Is the angle in radians or degrees? (r/d): ";
cin >> unit;
if (unit == 'd') {
angle = angle * M_PI / 180;
}
cout << "Do you want the result in radians or degrees? (r/d): ";
cin >> choice;
if (choice == 'd') {
result = sin(angle) * 180 / M_PI;
}
else {
result = sin(angle);
}
cout << result << endl;
}
else if (choice == 10) { // cos
double angle , result;
char unit, choice;
cout << "Enter an angle: ";
cin >> angle;
cout << "Is the angle in radians or degrees? (r/d): ";
cin >> unit;
if (unit == 'd') {
angle = angle * M_PI / 180;
}
cout << "Do you want the result in radians or degrees? (r/d): ";
cin >> choice;
if (choice == 'd') {
result = cos(angle) * 180 / M_PI;
}
else {
result = cos(angle);
}
cout << result << endl;
}
else if (choice == 11) { // tan
double angle , result;
char unit, choice;
cout << "Enter an angle: ";
cin >> angle;
cout << "Is the angle in radians or degrees? (r/d): ";
cin >> unit;
if (unit == 'd') {
angle = angle * M_PI / 180;
}
cout << "Do you want the result in radians or degrees? (r/d): ";
cin >> choice;
if (choice == 'd') {
result = tan(angle) * 180 / M_PI;
}
else {
result = tan(angle);
}
cout << result << endl;
}
else if (choice == 12) { // Inverse Sine (asin)
double value;
cout << "Enter a value between -1 and 1 to find its inverse sine: ";
cin >> value;
else {
cout << "Invalid input." << endl;
}
char response;
cout << "Do you want to perform another calculation? (Y/N): ";
cin >> response;
return 0;
}