Arithmetic Operators in Programming
Last Updated :
11 Jun, 2024
Arithmetic operators are fundamental components of programming languages that allow developers to perform mathematical operations on numerical data types. These operators enable manipulation of numeric values, making them essential for various computational tasks.
What are Arithmetic Operators?
Arithmetic operators are symbols used to perform mathematical calculations on numerical values. They operate on operands (variables or values) to produce a result. The basic arithmetic operators in most programming languages include addition (+), subtraction (-), multiplication (*), and division (/). Additionally, there are more advanced operators like modulus (%), and unary operators such as unary plus (+) and unary minus (-).
Here's a detailed overview of common arithmetic operators used in programming:
1. Addition (+)
The addition operator (+) is used to add two values together. It's one of the most fundamental arithmetic operations and is commonly used in calculations to find the sum of two or more numbers.
Example:
a = 10
b = 20
sum = a + b # Result: 30
2. Subtraction (-)
The subtraction operator (-) is used to subtract one value from another. It's used to find the difference between two numbers or to perform negation.
Example:
x = 50
y = 30
difference = x - y # Result: 20
3. Multiplication (*)
The multiplication operator (*) is used to multiply two values together. It's used in calculations to find the product of two or more numbers.
Example:
m = 5
n = 6
product = m * n # Result: 30
4. Division (/)
The division operator (/) is used to divide one value by another. It's used to find the quotient of two numbers. Note that division by zero may result in an error or undefined behavior depending on the programming language.
Example:
p = 20
q = 5
quotient = p / q # Result: 4.0
5. Modulus (%)
The modulus operator (%) returns the remainder of the division of one value by another. It's commonly used in programming tasks involving repetition, cycles, or determining if a number is even or odd.
Example:
numerator = 17
denominator = 5
remainder = numerator % denominator # Result: 2
6. Unary Plus (+) and Unary Minus (-)
The unary plus (+) and unary minus (-) are unary operators used to denote positivity or negativity of a value, respectively.
Example:
positive_num = +5 # Result: 5
negative_num = -8 # Result: -8
7. Increment (++) and Decrement (--)
The increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by one, respectively. Note: These operators may not be available in all programming languages.
Example:
counter = 10
counter++ # Increment
counter-- # Decrement
Implementation of Arithmetic Operators:
C++
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 3;
// Addition
int sum = a + b;
cout << "a + b = " << sum << endl;
// Subtraction
int diff = a - b;
cout << "a - b = " << diff << endl;
// Multiplication
int prod = a * b;
cout << "a * b = " << prod << endl;
// Division
if (b != 0) {
int quot = a / b;
cout << "a / b = " << quot << endl;
}
else {
cout << "Division by zero" << endl;
}
// Modulus
if (b != 0) {
int mod = a % b;
cout << "a % b = " << mod << endl;
}
else {
cout << "Modulus by zero" << endl;
}
// Unary Plus
int unaryPlus = +a;
cout << "+a = " << unaryPlus << endl;
// Unary Minus
int unaryMinus = -a;
cout << "-a = " << unaryMinus << endl;
// Increment (Pre-increment)
++a;
cout << "++a = " << a << endl;
// Decrement (Pre-decrement)
--a;
cout << "--a = " << a << endl;
// Increment (Post-increment)
int postInc = a++;
cout << "a++ = " << postInc << ", after a = " << a
<< endl;
// Decrement (Post-decrement)
int postDec = a--;
cout << "a-- = " << postDec << ", after a = " << a
<< endl;
return 0;
}
Java
public class Main {
public static void main(String[] args) {
int a = 10, b = 3;
// Addition
int sum = a + b;
System.out.println("a + b = " + sum);
// Subtraction
int diff = a - b;
System.out.println("a - b = " + diff);
// Multiplication
int prod = a * b;
System.out.println("a * b = " + prod);
// Division
if (b != 0) {
int quot = a / b;
System.out.println("a / b = " + quot);
} else {
System.out.println("Division by zero");
}
// Modulus
if (b != 0) {
int mod = a % b;
System.out.println("a % b = " + mod);
} else {
System.out.println("Modulus by zero");
}
// Unary Plus
int unaryPlus = +a;
System.out.println("+a = " + unaryPlus);
// Unary Minus
int unaryMinus = -a;
System.out.println("-a = " + unaryMinus);
// Increment (Pre-increment)
++a;
System.out.println("++a = " + a);
// Decrement (Pre-decrement)
--a;
System.out.println("--a = " + a);
// Increment (Post-increment)
int postInc = a++;
System.out.println("a++ = " + postInc + ", after a = " + a);
// Decrement (Post-decrement)
int postDec = a--;
System.out.println("a-- = " + postDec + ", after a = " + a);
}
}
Python
# Define the main function
def main():
a = 10
b = 3
# Addition
sum = a + b
print("a + b = " + str(sum))
# Subtraction
diff = a - b
print("a - b = " + str(diff))
# Multiplication
prod = a * b
print("a * b = " + str(prod))
# Division
if b != 0:
quot = a / b
print("a / b = " + str(quot))
else:
print("Division by zero")
# Modulus
if b != 0:
mod = a % b
print("a % b = " + str(mod))
else:
print("Modulus by zero")
# Unary Plus
unaryPlus = +a
print("+a = " + str(unaryPlus))
# Unary Minus
unaryMinus = -a
print("-a = " + str(unaryMinus))
# Increment (Pre-increment)
a += 1
print("++a = " + str(a))
# Decrement (Pre-decrement)
a -= 1
print("--a = " + str(a))
# Increment (Post-increment)
postInc = a
a += 1
print("a++ = " + str(postInc) + ", after a = " + str(a))
# Decrement (Post-decrement)
postDec = a
a -= 1
print("a-- = " + str(postDec) + ", after a = " + str(a))
# Call the main function
if __name__ == "__main__":
main()
Outputa + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
+a = 10
-a = -10
++a = 11
--a = 10
a++ = 10, after a = 11
a-- = 11, after a = 10
Conclusion:
Arithmetic operators in programming handle mathematical operations efficiently. They include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). Unary operators like unary plus (+) and unary minus (-) represent positive and negative values. Increment (++) and decrement (--) operators adjust values by one. Overall, arithmetic operators enable concise and effective mathematical computations in programming.
Similar Reads
What are Operators in Programming? Operators in programming are essential symbols that perform operations on variables and values, enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types. Operators in Programming Table of
15+ min read
Arithmetic Operators in Solidity Arithmetic operators are used to perform arithmetic or mathematical operations. Solidity has the following types of arithmetic operators: Addition: The addition operator takes two operands and results in a sum of these operands. It is denoted by +.Subtraction: The subtraction operator takes two oper
2 min read
Arithmetic Operators in Solidity Arithmetic operators are used to perform arithmetic or mathematical operations. Solidity has the following types of arithmetic operators: Addition: The addition operator takes two operands and results in a sum of these operands. It is denoted by +.Subtraction: The subtraction operator takes two oper
2 min read
Operator Associativity in Programming Operator associative refers to the order in which operators of the same precedence are used in a word. In a programming language, it is important to understand the interactions between operators to properly define and test expressions. In this article, we will discuss operator associativity in progr
14 min read
Arithmetic Operators in SQL Server Arithmetic operators play a crucial role in performing mathematical calculations within SQL Server. These operators allow you to perform addition, subtraction, multiplication, division, and more on numeric data stored in your database tables. In this article, we'll explore the various arithmetic ope
4 min read
VBA Arithmetic Operators in Excel Arithmetic Operators are the operators which perform mathematical operation or which perform any operation between two numbers like addition(+), subtraction(-), multiplication(*), division( / ), exponentiation(^), modulus(Mod), Bit-Shift operations. In this article, we will learn about the excel VBA
3 min read