Addition Operator
Dart Arithmetic Addition Operator takes two numbers as operands and returns their sum.
Symbol
+
symbol is used for Addition Operator.
Syntax
The syntax for Addition Operator is
</>
Copy
operand_1 + operand_2
The operands could be of any numeric datatype.
If the two operands are of different datatypes, implicit datatype promotion takes place and value of lower datatype is promoted to higher datatype.
Examples
Addition of Integers
In the following example, we read two integers from user, and add them using Arithmetic Addition Operator.
main.dart
</>
Copy
import 'dart:io';
void main() {
print('Enter n1');
var n1 = int.parse(stdin.readLineSync()!);
print('Enter n2');
var n2 = int.parse(stdin.readLineSync()!);
var output = n1 + n2;
print('n1 + n2 = $output');
}
Output
Enter n1
8
Enter n2
6
n1 + n2 = 14
Addition of Double Values
In the following example, we read two double values from user, and add them using Arithmetic Addition Operator.
main.dart
</>
Copy
import 'dart:io';
void main() {
print('Enter n1');
var n1 = double.parse(stdin.readLineSync()!);
print('Enter n2');
var n2 = double.parse(stdin.readLineSync()!);
var output = n1 + n2;
print('n1 + n2 = $output');
}
Output
Enter n1
5.3
Enter n2
6.1
n1 + n2 = 11.399999999999999
Conclusion
In this Dart Tutorial, we learned how to use Addition Operator to add given two numbers.