Scientific Calculator in C++

Build a menu-based scientific calculator in C++ with pow, sqrt, sin, cos, tan, and log. Covers degrees to radians, domain checks, and output formatting.

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}

Sample Output

Result: 256

pow(2, 8).

Exiting...

Choice 11.

In this one

  • How to call common <cmath> functions
  • Why trig functions need radians, not degrees
  • How to guard sqrt and log against invalid input
  • Why setprecision stays in effect after it is set

How it works

  1. 1

    Show the 11-option menu.

  2. 2

    Read choice (5).

  3. 3

    Read base 2 and exponent 8; pow gives 256.

  4. 4

    For trig choices, convert degrees with × M_PI / 180.

  5. 5

    Repeat until choice 11.

Edge cases

Sticky formatting

Pick sine, then addition.

fixed and setprecision(4) stay on cout, so the later sum prints as 30.0000. Save and restore the format, or set it each time.

tan(90°)

Choose tangent with 90.

90° in radians is not exactly π/2 in floating point, so tan returns a huge number like 16331239353195370 instead of an error.

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() {

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:

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 {

cout << "Error: Division by zero!" << endl;

}

Includes division by zero check
Prevents program crash
Provides error message

Power Operation

case 5:

cout << "Enter base and exponent: ";

cin >> num1 >> num2;

result = pow(num1, num2);

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

break;

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:

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;

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

cout << "Enter angle in degrees: ";

cin >> num1;

result = sin(num1 * M_PI / 180.0);

cout << fixed << setprecision(4);

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

break;

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:

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;

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

// ... more methods ...

};

Encapsulates operations in a class
Better code organization
Reusable calculator object

When to Use Scientific Calculator

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

sin(30) in C++ (no conversion) uses 30 as:

Pick an answer to check it.

Question 2

log(1) returns:

Pick an answer to check it.

Questions people ask

Is M_PI standard C++?

No. It is common on Linux and macOS but MSVC needs _USE_MATH_DEFINES. C++20 offers std::numbers::pi in <numbers>.

Which log is log()?

The natural log (base e). Use log10 for base 10.

Why check num1 >= 0 for sqrt?

sqrt of a negative double returns NaN; the check gives a friendlier message.