Arithmetic operators
- Dart course
- Beginner
- Lesson 24/116
- 4 min
Learn arithmetic with +, -, *, /, % and ~/.
Arithmetic operators
Dart has six basic arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulo/remainder) and ~/ (integer division).
/ always returns a double: 10 / 3 = 3.333.... If you want an integer result use ~/: 10 ~/ 3 = 3.
% gives the remainder of division: 10 % 3 = 1. It is often used for odd/even checks (x % 2) and in loops.
Using modulo (%)
The % operator gives the remainder of a division. For example 10 % 3 = 1 because 10 divided by 3 fits 3 times (9) and the remainder is 1.
Its most common use is odd/even checks: if x % 2 == 0 then x is even; if x % 2 == 1 it is odd.
It also helps in loops: to do something every third turn write if (i % 3 == 0). This is a handy way for periodic tasks.
Practice Three operations
Write print(10 + 5), print(10 - 3) and print(4 * 3) inside void main().
Starter code
// Print three arithmetic operationsSample solution
void main() {
print(10 + 5);
print(10 - 3);
print(4 * 3);
}Expected output
15 7 12
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Modulo operator
int kalan = 10 ____ 3;
int kalan = 10 % 3;
Explanation: % gives the remainder.
2 What is the result of 10 ~/ 3?
- 3.33
- Throws an error
- 3 Correct answer
- 4
Explanation: ~/ does integer division; it drops the fraction.
3 Match the operators
| Term | Meaning |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division (double) |