Menu
Coddy logo textTech

Integer Division

Part of the Fundamentals section of Coddy's Dart journey — lesson 17 of 94.

Dart's integer division (~/) returns an integer by truncating decimal parts, unlike regular division (/) which returns a double.

void main() {
  int num1 = 10;
  int num2 = 3;
  int result = num1 ~/ num2;
  
  print('Integer division: $num1 ~/ $num2 = $result');
}

Output: Integer division: 10 ~/ 3 = 3

void main() {
  int num1 = 10;
  int num2 = 3;
  
  double regularDivision = num1 / num2;
  int integerDivision = num1 ~/ num2;
  
  print('Regular division: $num1 / $num2 = $regularDivision');
  print('Integer division: $num1 ~/ $num2 = $integerDivision');
}
challenge icon

Challenge

Beginner

Create a program that demonstrates integer division in Dart:

  1. Declare two integer variables: totalApples with a value of 17 and numberOfPeople with a value of 5
  2. Calculate how many apples each person gets using integer division (~/) and store the result in a variable called applesPerPerson
  3. Calculate how many apples will be left over using the modulo operator (%) and store the result in a variable called remainingApples
  4. Print the results with the EXACT following format:
Each person gets: 3 apples
Remaining apples: 2

Your output must match this exact format.

Cheat sheet

Dart's integer division (~/) returns an integer by truncating decimal parts, unlike regular division (/) which returns a double.

int result = 10 ~/ 3;  // result = 3
double result = 10 / 3;  // result = 3.3333333333333335

Use the modulo operator (%) to get the remainder:

int remainder = 10 % 3;  // remainder = 1

Try it yourself

void main() {
  // Declare your variables here
  int totalApples = ?;
  int numberOfPeople = ?;
  
  // Calculate apples per person and remaining apples
  int applesPerPerson = ?;
  int remainingApples = ?;
  
  // Print the results
  print("Each person gets: $applesPerPerson apples");
  print("Remaining apples: $remainingApples");
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals