Arithmetic Operators in C++ are symbols employed to carry out mathematical computations. Additionally, these operators facilitate the incrementing and decrementing of variable values and can execute unary operations. In this article, we will explore the arithmetic operators in C++, their classifications, syntax, precedence, associativity, and examples within C++ programming.
Arithmetic operators in C++ refer to the operators utilized to conduct fundamental arithmetic tasks on variables or constants. They execute computations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Syntax of Arithmetic Operators in C++
variable = value1 arithmetic_operator value2;
Where:
value1 and value2 represent numerical values or variables.
arithmetic_operator denotes one of the arithmetic operators.
and variable holds the computed result.
Types of Arithmetic Operators in C++
Arithmetic operators in C++ can be categorized into three groups based on the operations they execute.
1. Basic Arithmetic Operators
These arithmetic operators in C++ are employed to perform fundamental mathematical calculations.
Operator Name
Symbol
Description
Syntax
Addition
+
Combines two numbers
a + b
Subtraction
–
Removes the second from the first
a – b
Multiplication
*
Multiplies two numbers
a * b
Division
/
Divides the first by the second (quotient)
a / b
Modulus
%
Returns the remainder from the division
a % b
2. Unary Arithmetic Operators
Unary arithmetic operators are those that function on a single operand. They accept just one value and perform actions on it.
Operator Name
Symbol
Description
Syntax
Unary Plus
+
Denotes a positive value (has no effect)
+a
Unary Minus
–
Negates the value (turns positive to negative and vice versa)
-a
3. Increment and Decrement Operators
These arithmetic operators are utilized to elevate and reduce the value of a variable by 1.
Operator Name
Symbol
Description
Syntax
Pre-Increment
++a
Increases value by 1 before usage
++a
Post-Increment
a++
Uses value first, then increases by 1
a++
Pre-Decrement
–a
Reduces the value by 1 prior to usage
–a
Post-Decrement
a–
Utilizes value first, then decreases by 1
a–
Basic Arithmetic Operators
Let’s briefly cover each of the basic arithmetic operators with examples in C++.
1. Addition (+)
The addition operator in C++ sums two numbers or operands.
Example:
“““html
Cpp
Code Copied!
var isMobile = window.innerWidth “);
editor69543.setValue(decodedContent); // Set the initial text
editor69543.clearSelection();
editor69543.setOptions({
maxLines: Infinity
});
function decodeHTML69543(input) {
var doc = new DOMParser().parseFromString(input, “text/html”);
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard69543() {
const code = editor69543.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(“.maineditor69543 .copymessage”).show();
setTimeout(function() {
jQuery(“.maineditor69543 .copymessage”).hide();
}, 2000);
}).catch(err => {
console.error(“Error copying code: “, err);
});
}
function runCode69543() {
var code = editor69543.getSession().getValue();
function closeoutput13500() {
var code = editor13500.getSession().getValue();
jQuery(".maineditor13500 .code-editor-output").hide();
}
// Add event listeners to the buttons
document.getElementById("copyBtn13500").addEventListener("click", copyCodeToClipboard13500);
document.getElementById("runBtn13500").addEventListener("click", runCode13500);
document.getElementById("closeoutputBtn13500").addEventListener("click", closeoutput13500);
Output:
The snippet illustrates how the subtraction operator is utilized to deduct two integers, a = 8 and b = 3, with the outcome of 5 being displayed as output in the console.
3. Multiplication (*)
The multiplication operator in C++ is employed to multiply two values.
Example:
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor21587.setValue(decodedContent); // Set the default text
editor21587.clearSelection();
editor21587.setOptions({
maxLines: Infinity
});
function decodeHTML21587(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard21587() {
const code = editor21587.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(".maineditor21587 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor21587 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error copying code: ", err);
});
}
function runCode21587() {
var code = editor21587.getSession().getValue();
The code demonstrates how the multiplication operator combines two integers, a and b, with values 8 and 5, subsequently printing the resulting 40 to the console.
4. Division (/)
The division operator in C++ performs division of the first value by the second, yielding the quotient as a result.
Example:
Cpp
Code Copied!
var isMobile = window.innerWidth
``````html
"");
editor84296.setValue(decodedContent); // Initialize the default text
editor84296.clearSelection();
editor84296.setOptions({
maxLines: Infinity
});
function decodeHTML84296(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to the clipboard
function copyCodeToClipboard84296() {
const code = editor84296.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput84296() {
var code = editor84296.getSession().getValue();
jQuery(".maineditor84296 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn84296").addEventListener("click", copyCodeToClipboard84296);
document.getElementById("runBtn84296").addEventListener("click", runCode84296);
document.getElementById("closeoutputBtn84296").addEventListener("click", closeoutput84296);
Output:
The code illustrates how the division operator accomplishes the division of integer a = 20 by integer b = 4, with the output of 5 displayed in the console.
5. Modulus (%) Operator
The modulus operator in C++ provides the remainder from dividing two integers. It exclusively operates on integer data types.
Example:
Cpp
Code Copied!
var isMobile = window.innerWidth "");
editor92797.setValue(decodedContent); // Initialize the default text
editor92797.clearSelection();
editor92797.setOptions({
maxLines: Infinity
});
function decodeHTML92797(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard92797() {
const code = editor92797.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput92797() {
var code = editor92797.getSession().getValue();
jQuery(".maineditor92797 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn92797").addEventListener("click", copyCodeToClipboard92797);
document.getElementById("runBtn92797").addEventListener("click", runCode92797);
document.getElementById("closeoutputBtn92797").addEventListener("click", closeoutput92797);
Output:
The code demonstrates how the modulus operator works to obtain the...
``````html
The remainder is 2 when the two integers a = 20 and b = 3 are divided.
Priority and Evaluation of Arithmetic Operators in C++
The priority determines the sequence of operations executed by the operator, while the evaluation of an operator ascertains the sequence in which that operator is processed.
Operator
Type of Operator
Priority
Evaluation
+
Unary Plus
Highest
Right to Left
–
Unary Minus
Highest
Right to Left
*
Multiplication
Highest
Left to Right
/
Division
High
Left to Right
%
Modulus
High
Left to Right
+
Addition
Low
Left to Right
–
Subtraction
Lowest
Left to Right
C++ Illustration of Unary Arithmetic Operators
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor36042.setValue(decodedContent); // Set the default text
editor36042.clearSelection();
editor36042.setOptions({
maxLines: Infinity
});
function decodeHTML36042(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard36042() {
const code = editor36042.getValue(); // Get code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(".maineditor36042 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor36042 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error copying code: ", err);
});
}
function runCode36042() {
var code = editor36042.getSession().getValue();
function closeoutput36042() {
jQuery(".maineditor36042 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn36042").addEventListener("click", copyCodeToClipboard36042);
document.getElementById("runBtn36042").addEventListener("click", runCode36042);
document.getElementById("closeoutputBtn36042").addEventListener("click", closeoutput36042);
Result:
The snippet illustrates how the unary plus operator retains the value of a, whereas the unary minus operator inverts the value of a.
C++ Illustration of Increment and Decrement Operators
Cpp
Code Copied!
// Rest of the script...
``````javascript
"");
editor55753.setValue(decodedContent); // Set initial text
editor55753.clearSelection();
editor55753.setOptions({
maxLines: Infinity
});
function decodeHTML55753(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard55753() {
const code = editor55753.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
The code illustrates how ++a and –a first modify the values and then assign them, while a++ and a– first assign the value and subsequently modify it. The results of all operations are then displayed in the console.
Conclusion
Arithmetic operators in C++ are crucial for executing various types of mathematical tasks, such as addition and subtraction. These operators can also be utilized to increment and decrement the values of variables. Moreover, they have a defined order of precedence and associativity. By comprehending how these operators function along with their precedence and associativity, one can easily conduct arithmetic calculations in C++ programming.
FAQs
Q1. What occurs if I divide a number by zero?
Dividing a number by zero will result in a runtime error.
Q2. Can modulus be applied to floating-point numbers?
No, the modulus operator cannot be used with floating-point numbers. Therefore, you should use fmod() from <cmath> for floating points.
Q3. What differentiates a++ from ++a?
The distinction between a++ and ++a is that a++ returns the value first, followed by incrementing, whereas ++a increments first.
Q4. Why does integer division ignore decimals?
Integer division retains only the whole number.
Q5. How can you alter operator precedence?
To change the operator precedence, use parentheses ().
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.