Skip to main content

Command Palette

Search for a command to run...

Dart Operators: Arithmetic

Published
2 min read
Dart Operators: Arithmetic
P

I'm Computer Engineering Student. Learning Status DSA in Java, Dart Development, Python in Django Development. Best part is Communication Skills, Marketing, Accounting.

In Dart, arithmetic operators are used to perform mathematical calculations on numerical values. Here are the arithmetic operators available in Dart:

  1. Addition (+):

    • Adds two operands.

    • Example: int sum = 3 + 5; // sum is 8

  2. Subtraction (-):

    • Subtracts the right operand from the left operand.

    • Example: int difference = 8 - 3; // difference is 5

  3. Multiplication (*):

    • Multiplies two operands.

    • Example: int product = 3 * 5; // product is 15

  4. Division (/):

    • Divides the left operand by the right operand.

    • If both operands are integers, the result is an integer (integer division), and any fractional part is discarded.

    • If at least one operand is a double, the result is a double.

    • Example: double quotient = 10 / 3; // quotient is 3.3333...

  5. Modulo (%):

    • Returns the remainder of the division of the left operand by the right operand.

    • Example: int remainder = 10 % 3; // remainder is 1

Here's a simple example demonstrating the use of arithmetic operators in Dart:

void main() {
  int a = 10;
  int b = 3;

  int sum = a + b; // 10 + 3 = 13
  int difference = a - b; // 10 - 3 = 7
  int product = a * b; // 10 * 3 = 30
  double quotient = a / b; // 10 / 3 = 3.3333...
  int remainder = a % b; // 10 % 3 = 1

  print('Sum: $sum');
  print('Difference: $difference');
  print('Product: $product');
  print('Quotient: $quotient');
  print('Remainder: $remainder');
}

Output:

Sum: 13
Difference: 7
Product: 30
Quotient: 3.3333333333333335
Remainder: 1

These arithmetic operators are fundamental for performing mathematical calculations in Dart programs.

More from this blog

Parth Chauhan's blog

87 posts

Learner, Accountant, Marketer, Programmer.