Menu
CoddyTech

Integer Division

Part of the First Circuits section of Coddy's Arduino journey. Lesson 15 of 22.

When both sides of / are whole numbers, the answer is a whole number too. The fraction is dropped, not rounded: 7 / 2 is 3, and 19 / 10 is 1.

The part that was dropped is what % gives you: 7 % 2 is 1. Together they answer "how many fit, and how many are left over".

Serial.println(7 / 2);    // 3
Serial.println(7 % 2);    // 1
Serial.println(7.0 / 2);  // 3.50

To keep the fraction, write one of the numbers with a decimal point. 7.0 is a decimal number, and dividing with a decimal number gives a decimal answer: 3.5. Serial.println() shows decimal numbers with two digits after the point, so it prints 3.50.

challenge icon

Challenge

Easy

17 sweets are shared between 5 children. In setup(), print three lines:

  • how many sweets each child gets: 17 / 5
  • how many are left over: 17 % 5
  • the exact share as a decimal: 17.0 / 5

Press Run, then Check.

Try it yourself

void setup() {
  Serial.begin(9600);
  // each child's share, what is left over, the exact share
}

void loop() {
}
quiz iconTest yourself

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

All lessons in First Circuits

Practice on your own: Arduino playground