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 <iostream>
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 ifnum2
is zero. - Example Usage: Variables
num1
andnum2
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 usingcout
.
Usage:
To use this calculator program:
- Edit the values of
num1
andnum2
to input different numbers. - 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:
- Function Definitions:
- Functions (
add
,subtract
,multiply
,divide
) are explained with their roles in performing arithmetic operations in C++.
- Functions (
- Function Usage:
- Each function is used to perform addition, subtraction, multiplication, and division operations within the
main
function.
- Each function is used to perform addition, subtraction, multiplication, and division operations within the
- Error Handling:
- The
divide
function checks for division by zero and prints an error message usingcout
ifnum2
is zero.
- The
- Example Usage:
- Variables
num1
andnum2
are initialized with example values, and the functions (add
,subtract
,multiply
,divide
) are called to demonstrate arithmetic operations.
- Variables
- Output:
- The results of each operation (
sum
,difference
,product
,quotient
) are printed to the console usingcout
.
- The results of each operation (
- 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.