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

Calculator Project by If Statement

The document contains code for a C++ program that acts as a scientific calculator. It displays a menu with 18 math operation options for the user to select from including trigonometric, logarithmic, exponential and other functions. The user selects an operation and inputs values if needed, then the program performs the calculation and displays the result. It repeats this process, allowing the user to continuously select new operations, until they choose to quit.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Calculator Project by If Statement

The document contains code for a C++ program that acts as a scientific calculator. It displays a menu with 18 math operation options for the user to select from including trigonometric, logarithmic, exponential and other functions. The user selects an operation and inputs values if needed, then the program performs the calculation and displays the result. It repeats this process, allowing the user to continuously select new operations, until they choose to quit.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#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;

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;

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


cout << "The inverse sine is not defined for values outside the range [-1,
1]!" << endl;
} else {
cout << "Inverse sine of " << value << " = " << asin(value) << " radians" <<
endl;
}
}
else if (choice == 13) { // Inverse Cosine (acos)
double value;
cout << "Enter a value between -1 and 1 to find its inverse cosine: ";
cin >> value;

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


cout << "The inverse cosine is not defined for values outside the range [-
1, 1]!" << endl;
} else {
cout << "Inverse cosine of " << value << " = " << acos(value) << " radians"
<< endl;
}
}
else if (choice == 14) { // Inverse Tangent (atan)
double value;
cout << "Enter a value to find its inverse tangent: ";
cin >> value;
cout << "Inverse tangent of " << value << " = " << atan(value) << " radians"
<< endl;
}
else if (choice == 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;
}
else if (choice == 16) { // Exponent (exp)
double num;
cout << "Enter a number to find its exponent: ";
cin >> num;
cout << "Exponential value of " << num << " = " << exp(num) << endl;
}
else if (choice == 17)
{
double angle , sinh;
cout << "Enter the angle in degrees: ";
cin >> angle;
sinh = (exp(angle) - exp(-angle)) / 2;
cout << "sinh(" << angle << ") = " << sinh << endl;
}
else if (choice == 18)
{
double angle , cosh;
cout << "Enter the angle in degrees: ";
cin >> angle;
cosh = (exp(angle) + exp(-angle)) / 2;
cout << "cosh(" << angle << ") = " << cosh << endl;
}

else {
cout << "Invalid input." << endl;
}

char response;
cout << "Do you want to perform another calculation? (Y/N): ";
cin >> response;

if (response != 'Y' && response != 'y') {


break;
}
}

return 0;
}

You might also like