Scientific Calculator in C++

Scientific Calculator in C++ (3 Programs).

IntermediateApplication ProgramsExample 2 of 3
scientific-calculator.cpp
1#include <iostream>
2#include <cmath>
3#include <iomanip>
4using namespace std;
5
6void displayMenu() {
7 cout << "\n=== SCIENTIFIC CALCULATOR ===" << endl;
8 cout << "1. Addition" << endl;
9 cout << "2. Subtraction" << endl;
10 cout << "3. Multiplication" << endl;
11 cout << "4. Division" << endl;
12 cout << "5. Power" << endl;
13 cout << "6. Square Root" << endl;
14 cout << "7. Sine" << endl;
15 cout << "8. Cosine" << endl;
16 cout << "9. Tangent" << endl;
17 cout << "10. Logarithm" << endl;
18 cout << "11. Exit" << endl;
19 cout << "Enter your choice: ";
20}
21
22int main() {
23 int choice;
24 double num1, num2, result;
25
26 do {
27 displayMenu();
28 cin >> choice;
29
30 switch(choice) {
31 case 1:
32 cout << "Enter two numbers: ";
33 cin >> num1 >> num2;
34 result = num1 + num2;
35 cout << "Result: " << result << endl;
36 break;
37 case 2:
38 cout << "Enter two numbers: ";
39 cin >> num1 >> num2;
40 result = num1 - num2;
41 cout << "Result: " << result << endl;
42 break;
43 case 3:
44 cout << "Enter two numbers: ";
45 cin >> num1 >> num2;
46 result = num1 * num2;
47 cout << "Result: " << result << endl;
48 break;
49 case 4:
50 cout << "Enter two numbers: ";
51 cin >> num1 >> num2;
52 if (num2 != 0) {
53 result = num1 / num2;
54 cout << "Result: " << result << endl;
55 } else {
56 cout << "Error: Division by zero!" << endl;
57 }
58 break;
59 case 5:
60 cout << "Enter base and exponent: ";
61 cin >> num1 >> num2;
62 result = pow(num1, num2);
63 cout << "Result: " << result << endl;
64 break;
65 case 6:
66 cout << "Enter a number: ";
67 cin >> num1;
68 if (num1 >= 0) {
69 result = sqrt(num1);
70 cout << "Result: " << result << endl;
71 } else {
72 cout << "Error: Cannot find square root of negative number!" << endl;
73 }
74 break;
75 case 7:
76 cout << "Enter angle in degrees: ";
77 cin >> num1;
78 result = sin(num1 * M_PI / 180.0);
79 cout << fixed << setprecision(4);
80 cout << "Result: " << result << endl;
81 break;
82 case 8:
83 cout << "Enter angle in degrees: ";
84 cin >> num1;
85 result = cos(num1 * M_PI / 180.0);
86 cout << fixed << setprecision(4);
87 cout << "Result: " << result << endl;
88 break;
89 case 9:
90 cout << "Enter angle in degrees: ";
91 cin >> num1;
92 result = tan(num1 * M_PI / 180.0);
93 cout << fixed << setprecision(4);
94 cout << "Result: " << result << endl;
95 break;
96 case 10:
97 cout << "Enter a number: ";
98 cin >> num1;
99 if (num1 > 0) {
100 result = log(num1);
101 cout << "Result: " << result << endl;
102 } else {
103 cout << "Error: Logarithm undefined for non-positive numbers!" << endl;
104 }
105 break;
106 case 11:
107 cout << "Exiting..." << endl;
108 break;
109 default:
110 cout << "Invalid choice!" << endl;
111 }
112 } while(choice != 11);
113
114 return 0;
115}

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...

What's going on

The program creates a scientific calculator that performs:

Basic arithmetic: addition, subtraction, multiplication, division
Advanced operations: power, square root
Trigonometric functions: sine, cosine, tangent
Logarithmic functions: natural logarithm

Users can select operations from a menu and perform calculations interactively.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <cmath>
Provides mathematical functions: pow(), sqrt(), sin(), cos(), tan(), log()
Contains M_PI constant for pi value
Essential for scientific calculations
3.#include <iomanip>
Provides setprecision() and fixed for formatting decimal output
Important for displaying results with specific decimal places

Understanding Scientific Calculator

Mathematical Functions

:

Arithmetic: +, -, *, /
Power: base^exponent
Square Root: √x
Trigonometry: sin, cos, tan (angles in degrees or radians)
Logarithms: natural log (ln)

Angle Conversion

:

Trigonometric functions in C++ use radians
User input in degrees must be converted: radians = degrees × π/180
M_PI constant provides π value

Function: displayMenu()

void displayMenu() {

}

1cout << "\n=== SCIENTIFIC CALCULATOR ===" << endl;
2cout << "1. Addition" << endl;
3cout << "2. Subtraction" << endl;
4// ... more options ...
5cout << "11. Exit" << endl;
6cout << "Enter your choice: ";

How it works

:

Displays all available mathematical operations
Numbers 1-11 represent different operations
Clear formatting for user-friendly interface

Declaring Variables

The program declares:

int choice;

double num1, num2, result;

choice stores the user's menu selection
num1, num2 store input numbers for calculations
result stores the calculation result
double is used for precision in mathematical operations

Basic Arithmetic Operations

Addition, Subtraction, Multiplication

:

Standard arithmetic operations
Require two numbers as input
Straightforward calculations

Division

:

if (num2 != 0) {

result = num1 / num2;

} else {

}

Includes division by zero check
Prevents program crash
Provides error message
1cout << "Error: Division by zero!" << endl;

Power Operation

case 5:

cin >> num1 >> num2;

result = pow(num1, num2);

cout << "Result: " << result << endl;

break;

1cout << "Enter base and exponent: ";

How it works

:

pow(base, exponent) calculates base^exponent
Example: pow(2, 3) = 2³ = 8
Handles both integer and decimal exponents

Square Root Operation

case 6:

cin >> num1;

if (num1 >= 0) {

result = sqrt(num1);

cout << "Result: " << result << endl;

} else {

cout << "Error: Cannot find square root of negative number!" << endl;

}

break;

1cout << "Enter a number: ";

How it works

:

sqrt(x) calculates square root of x
Only works for non-negative numbers
Includes validation for negative input
Example: sqrt(16) = 4

Trigonometric Functions

case 7: // Sine

cin >> num1;

result = sin(num1 * M_PI / 180.0);

cout << fixed << setprecision(4);

cout << "Result: " << result << endl;

break;

1cout << "Enter angle in degrees: ";

How it works

:

sin(), cos(), tan() functions use radians
Convert degrees to radians: degrees × π/180
M_PI is the constant for π (approximately 3.14159)
setprecision(4) displays 4 decimal places

Example

:

Input: 30 degrees
Conversion: 30 × π/180 = π/6 radians
sin(π/6) = 0.5

Logarithmic Function

case 10:

cin >> num1;

if (num1 > 0) {

result = log(num1);

cout << "Result: " << result << endl;

} else {

cout << "Error: Logarithm undefined for non-positive numbers!" << endl;

}

break;

1cout << "Enter a number: ";

How it works

:

log(x) calculates natural logarithm (ln)
Only defined for positive numbers
Includes validation for non-positive input
Example: log(e) = 1, where e ≈ 2.718

Other Methods (Mentioned but not shown in code)

Method 2: Advanced Version

Includes more operations: factorial, percentage, etc.
More comprehensive mathematical functions
Extended menu options

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); }

};

Encapsulates operations in a class
Better code organization
Reusable calculator object
1// ... more methods ...

When to Use Scientific Calculator