c++ code for making scientific calculator
c++ code for making scientific calculator
Certainly! Here's a simple C++ code for a basic scientific calculator that can perform operations like addition, subtraction, multiplication, division, and trigonometric functions (sine, cosine, and tangent). This code uses the <cmath> library for trigonometric functions.
cpp
Copy code
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int choice;
double num1, num2;
cout << "Scientific Calculator" << endl;
cout << "1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Sine\n6. Cosine\n7. Tangent" << endl;
cout << "Enter your choice (1-7): ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Result: " << num1 + num2 << endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Result: " << num1 - num2 << endl;
break;
case 3:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Result: " << num1 * num2 << endl;
break;
case 4:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
if (num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "Error: Division by zero" << endl;
break;
case 5:
cout << "Enter an angle in radians: ";
cin >> num1;
cout << "Result: " << sin(num1) << endl;
break;
case 6:
cout << "Enter an angle in radians: ";
cin >> num1;
cout << "Result: " << cos(num1) << endl;
break;
case 7:
cout << "Enter an angle in radians: ";
cin >> num1;
cout << "Result: " << tan(num1) << endl;
break;
default:
cout << "Invalid choice" << endl;
}
return 0;
}
This is a basic example, and you can extend it to include more advanced operations and error handling as needed. Remember that this code assumes input in radians for trigonometric functions.
Post a Comment