Subtraction Operator

Dart Arithmetic Subtraction Operator takes two numbers as operands, subtracts the second number from the first number, and returns the result.

Symbol

- symbol is used for Subtraction Operator.

ADVERTISEMENT

Syntax

The syntax for Subtraction Operator is

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

Subtraction of Integers

In the following example, we read two integers from user, and find their difference them using Arithmetic Subtraction Operator.

main.dart

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
3
n1 - n2 = 5

Subtraction of Double Values

In the following example, we read two double values from user, and find their difference using Arithmetic Subtraction Operator.

main.dart

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
3.14
Enter n2
1.245
n1 - n2 = 1.895

Conclusion

In this Dart Tutorial, we learned how to use Subtraction Operator to find the difference of given numbers.