Scientific Calculator
Scientific Calculator in C++ (3 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;
}=== 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 teaches you how to build a scientific calculator in C++ that performs advanced mathematical operations beyond basic arithmetic. A scientific calculator includes trigonometric functions (sine, cosine, tangent), logarithmic functions, power operations, square roots, and more. This program demonstrates how to use mathematical libraries and handle various mathematical operations in a menu-driven interface.
---
1. What This Program Does
The program creates a scientific calculator that performs:
Users can select operations from a menu and perform calculations interactively.
---
2. Header Files Used
---
3. Understanding Scientific Calculator
Mathematical Functions
:
Angle Conversion
:
---
4. Function: displayMenu()
void displayMenu() {
}
cout << "\n=== SCIENTIFIC CALCULATOR ===" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
// ... more options ...
cout << "11. Exit" << endl;
cout << "Enter your choice: ";How it works
:
---
5. Declaring Variables
The program declares:
int choice;
double num1, num2, result;
---
6. Basic Arithmetic Operations
Addition, Subtraction, Multiplication
:
Division
:
if (num2 != 0) {
result = num1 / num2;
} else {
}
---
cout << "Error: Division by zero!" << endl;7. Power Operation
case 5:
cin >> num1 >> num2;
result = pow(num1, num2);
break;
cout << "Enter base and exponent: ";How it works
:
---
8. Square Root Operation
case 6:
cin >> num1;
if (num1 >= 0) {
result = sqrt(num1);
} else {
}
break;
cout << "Enter a number: ";How it works
:
---
9. Trigonometric Functions
cin >> num1;
result = sin(num1 * M_PI / 180.0);
cout << fixed << setprecision(4);
break;
cout << "Enter angle in degrees: ";How it works
:
Example
:
---
10. Logarithmic Function
case 10:
cin >> num1;
if (num1 > 0) {
result = log(num1);
} else {
}
break;
cout << "Enter a number: ";How it works
:
---
11. Other Methods (Mentioned but not shown in code)
Method 2: Advanced Version
Method 3: Object-Oriented Version
class ScientificCalculator {
double add(double a, double b) { return a + b; }
double power(double base, double exp) { return pow(base, exp); }
};
---
// ... more methods ...12. When to Use Scientific Calculator
Real-World Applications
:
Learning Purposes
:
---
13. Important Considerations
Input Validation
:
Precision
:
Angle Units
:
---
14. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning mathematical libraries, understanding function usage, and preparing for building scientific and engineering applications in C++ programs.
Let us now understand every line and the components of the above program.
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.
Practical Learning Notes for Scientific Calculator
This C++ program is part of the "Application Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.