Simple Calculator Program in C++

 

Simple Calculator Program in C++

This C++ program implements a basic calculator with operations for addition, subtraction, multiplication, and division.

C++ Code:


/*
A simple calculator program in C++.
*/

#include &ltiostream&gt

using namespace std;

// Function to add two numbers
double add(double num1, double num2) {
    return num1 + num2;
}

// Function to subtract two numbers
double subtract(double num1, double num2) {
    return num1 - num2;
}

// Function to multiply two numbers
double multiply(double num1, double num2) {
    return num1 * num2;
}

// Function to divide two numbers
double divide(double num1, double num2) {
    if (num2 == 0) {
        cout << "Error: Cannot divide by zero" << endl;
        return 0;
    }
    return num1 / num2;
}

int main() {
    // Example usage
    double num1 = 10.5;
    double num2 = 5.2;
    
    // Perform operations
    double sum = add(num1, num2);
    double difference = subtract(num1, num2);
    double product = multiply(num1, num2);
    double quotient = divide(num1, num2);
    
    // Output results
    cout << "Sum: " << sum << endl;
    cout << "Difference: " << difference << endl;
    cout << "Product: " << product << endl;
    cout << "Quotient: " << quotient << endl;
    
    return 0;
}
  

Explanation:

The C++ code above defines a simple calculator program with the following components:

  • Function Definitions: Functions (add, subtract, multiply, divide) are defined to perform basic arithmetic operations.
  • Function Usage: Each function is used to perform addition, subtraction, multiplication, and division operations.
  • Error Handling: The divide function checks for division by zero and prints an error message if num2 is zero.
  • Example Usage: Variables num1 and num2 are initialized with example values, and the functions are called to perform arithmetic operations.
  • Output: The results of each operation (sum, difference, product, quotient) are printed to the console using cout.

Usage:

To use this calculator program:

  1. Edit the values of num1 and num2 to input different numbers.
  2. Compile the C++ program using a C++ compiler (e.g., g++) and run the executable to perform addition, subtraction, multiplication, and division operations based on the input values.

 

Explanation:

  1. Function Definitions:
    • Functions (add, subtract, multiply, divide) are explained with their roles in performing arithmetic operations in C++.
  2. Function Usage:
    • Each function is used to perform addition, subtraction, multiplication, and division operations within the main function.
  3. Error Handling:
    • The divide function checks for division by zero and prints an error message using cout if num2 is zero.
  4. Example Usage:
    • Variables num1 and num2 are initialized with example values, and the functions (add, subtract, multiply, divide) are called to demonstrate arithmetic operations.
  5. Output:
    • The results of each operation (sum, difference, product, quotient) are printed to the console using cout.
  6. Usage Instructions:
    • Instructions are provided on how to use the calculator program by editing the input variables, compiling the C++ program using a C++ compiler, and running the executable to perform arithmetic calculations.

Leave a Reply

Your email address will not be published. Required fields are marked *