C++ arithmetic operators
Created By: chatGPT
In C++, arithmetic operators are used to perform basic mathematical operations on numerical values. The common arithmetic operators include addition, subtraction, multiplication, division, and modulus. These operators can be applied to both integers and floating-point numbers. Here's a brief overview of each operator:include
In this example, the program computes the sum, difference, product, quotient, and remainder of two integers,
1. Addition (
+
): This operator adds two values together.int sum = a + b;
2. Subtraction (
-
): This operator subtracts one value from another.int difference = a - b;
3. Multiplication (
*
): This operator multiplies two values.int product = a * b;
4. Division (
/
): This operator divides one value by another. Be cautious with integer division as it will discard the decimal part.int quotient = a / b;
5. Modulus (
Here is an example that demonstrates the use of these operators:%
): This operator returns the remainder after division of one number by another.int remainder = a % b;
cpp
include
int main() { int a = 10; int b = 3;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
std::cout << "Quotient: " << quotient << std::endl;
std::cout << "Remainder: " << remainder << std::endl;
return 0;
}
a
and b
, and outputs the results to the console. Make sure to understand the implications of using division with integers, as it can lead to unexpected results if not managed correctly. For floating-point numbers, the division will yield a more precise result.