Dart Arithmetic Operators
Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc.
They are binary operators that work upon two operands.
Consider A = 15, B = 10 for the following table :
S. No. |
Operator Name |
Description |
Example |
1. |
Addition (+) |
It adds the left operand to the right operand. |
A + B will return 25 |
2. |
Subtraction (-) |
It subtracts the right operand from the left operand. |
A - B will return 5 |
3 |
Divide (/) |
It divides the first operand by the second operand and returns the
double type quotient. |
A / B will return 1.5 |
4. |
Multiplication (*) |
It multiplies the one operand to another operand. |
A * B will return 150 |
5. |
Modulus (%) |
It returns a remainder after dividing one operand from another. |
A % B will return 5 |
6. |
Division (~ /) |
It divides the first operand by the second operand and returns integer
type quotient. |
A ~ / B will return 1 |
7. |
Unary Minus (-expr) |
It is used with a single operand changes the sign of it. |
- ( A – B ) will return -5 |
Program
Consider the following code in Dart that explains the concept of arithmetic operators
void main( )
{
int a = 157, b = 130 ;
// addition operator ( + )
int sum = a + b ;
print( ' Addition of two numbers is : $sum ' ) ;
// subtraction operator ( - )
int diff = a - b ;
print( ' Subtraction of two numbers is : $diff ' ) ;
// Multiplication operator ( * )
int prod = a * b ;
print( ' Product of two numbers is : $prod ' ) ;
// Division operator ( ~/ ) that returns integer type quotient
int quo = a ~/ b ;
print( ' Division of two numbers is : $quo ' ) ;
// Divide operator ( / )that returns double type quotient
double quo1 = a / b ;
print( ' Division of two numbers is : $quo1 ' ) ;
// Modulus operator ( % )
int rem = a % b ;
print( ' Remainder of two numbers is : $rem ' ) ;
// Unary minus operator ( - )
int res = -sum ;
print( ' Negative of addition of two numbers is : $res ' ) ;
}
Output
Addition of two numbers is : 287
Subtraction of two numbers is : 27
Product of two numbers is : 20410
Division of two numbers is : 1
Division of two numbers is : 1.2076923076923076
Remainder of two numbers is : 27
Negative of addition of two numbers is : -287
Remember the following points :
- Division Operator ( ~/ ) returns the quotient of integer type.
- Divide Operator ( / ) returns the quotient of double type.
- Unary minus ( - ) is the only arithmetic operator that works on a single operand.