Scientific Calculator
Scientific Calculator in C++ (3 Programs)
IntermediateTopic: Application Programs
C++ Scientific Calculator Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void displayMenu() {
cout << "\n=== SCIENTIFIC CALCULATOR ===" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "5. Power" << endl;
cout << "6. Square Root" << endl;
cout << "7. Sine" << endl;
cout << "8. Cosine" << endl;
cout << "9. Tangent" << endl;
cout << "10. Logarithm" << endl;
cout << "11. Exit" << endl;
cout << "Enter your choice: ";
}
int main() {
int choice;
double num1, num2, result;
do {
displayMenu();
cin >> choice;
switch(choice) {
case 1:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 - num2;
cout << "Result: " << result << endl;
break;
case 3:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case 4:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
if (num2 != 0) {
result = num1 / num2;
cout << "Result: " << result << endl;
} else {
cout << "Error: Division by zero!" << endl;
}
break;
case 5:
cout << "Enter base and exponent: ";
cin >> num1 >> num2;
result = pow(num1, num2);
cout << "Result: " << result << endl;
break;
case 6:
cout << "Enter a number: ";
cin >> num1;
if (num1 >= 0) {
result = sqrt(num1);
cout << "Result: " << result << endl;
} else {
cout << "Error: Cannot find square root of negative number!" << endl;
}
break;
case 7:
cout << "Enter angle in degrees: ";
cin >> num1;
result = sin(num1 * M_PI / 180.0);
cout << fixed << setprecision(4);
cout << "Result: " << result << endl;
break;
case 8:
cout << "Enter angle in degrees: ";
cin >> num1;
result = cos(num1 * M_PI / 180.0);
cout << fixed << setprecision(4);
cout << "Result: " << result << endl;
break;
case 9:
cout << "Enter angle in degrees: ";
cin >> num1;
result = tan(num1 * M_PI / 180.0);
cout << fixed << setprecision(4);
cout << "Result: " << result << endl;
break;
case 10:
cout << "Enter a number: ";
cin >> num1;
if (num1 > 0) {
result = log(num1);
cout << "Result: " << result << endl;
} else {
cout << "Error: Logarithm undefined for non-positive numbers!" << endl;
}
break;
case 11:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
} while(choice != 11);
return 0;
}Output
=== SCIENTIFIC CALCULATOR === 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Power 6. Square Root 7. Sine 8. Cosine 9. Tangent 10. Logarithm 11. Exit Enter your choice: 5 Enter base and exponent: 2 8 Result: 256 === SCIENTIFIC CALCULATOR === ... Enter your choice: 11 Exiting...
Understanding Scientific Calculator
This program demonstrates 3 different implementations of a scientific calculator: basic version with arithmetic and trigonometric functions, advanced version with more mathematical operations, and object-oriented version using classes.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.