Scientific Calculator

Scientific Calculator in C++ (3 Programs)

C++Intermediate
C++
#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...

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:

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


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

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

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:

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

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

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

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

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

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

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

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

12. When to Use Scientific Calculator

Real-World Applications:

  • Engineering calculations
  • Scientific computations
  • Educational tools
  • Mathematical software

Learning Purposes:

  • Understanding mathematical libraries
  • Learning function usage
  • Practicing menu-driven programs
  • Building complete applications

13. Important Considerations

Input Validation:

  • Division by zero check
  • Negative number check for square root
  • Non-positive number check for logarithm
  • Invalid input handling

Precision:

  • Use double for decimal precision
  • setprecision() for output formatting
  • Consider floating-point limitations

Angle Units:

  • C++ trigonometric functions use radians
  • Always convert degrees to radians
  • Use M_PI constant for conversion

14. return 0;

This ends the program successfully.


Summary

  • Scientific calculator performs advanced mathematical operations beyond basic arithmetic.
  • Uses <cmath> library for mathematical functions: pow(), sqrt(), sin(), cos(), tan(), log().
  • Trigonometric functions require angle conversion: degrees × π/180 to radians.
  • Input validation is essential: division by zero, negative square root, non-positive logarithm.
  • setprecision() formats decimal output for readability.
  • Understanding mathematical libraries and function usage is essential.
  • Multiple implementations exist: basic, advanced, object-oriented.
  • Scientific calculator demonstrates real-world application development.

This program is fundamental for beginners learning mathematical libraries, understanding function usage, and preparing for building scientific and engineering applications in C++ programs.