Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Programming Fundamentals · Grade 6-8

Integer

A data type that represents whole numbers (positive, negative, or zero) without decimal points.


Quick Answer

A data type that represents whole numbers (positive, negative, or zero) without decimal points. Use an integer for anything you count or number in whole steps: players in a lobby, lives left, a position in a list, a temperature in whole degrees. Integer arithmetic is exact, but division needs care: integer division keeps only the whole part (7 // 2 is 3 in Python) and the remainder operator gives what is left over (7 % 2 is 1). A value like 9.99 needs a float or decimal type instead.

Why This Matters

Integers make counting, list positions and splitting into groups reliable. Knowing how they divide answers questions like how many 40-seat buses 130 students need: 130 // 40 = 3 full buses with 130 % 40 = 10 students left over, so 4 buses. Treat integers carelessly and you get surprises: an average that silently loses its fraction, or a price whose cents vanish.

Intuitive Explanation

Integers are the tick marks on a number line, ..., -2, -1, 0, 1, 2, ..., with nothing in between. They are what you get when you count things that cannot be split: 23 students, 0 messages, -4 degrees. A computer stores each one exactly, so adding, subtracting and multiplying integers never introduces a rounding error: 1000000 + 1 is exactly 1000001.

Division is where integers behave differently from the fractions you learned in math. Share 17 stickers among 5 friends: each gets 3 and 2 are left over. Integer division gives the 3 (17 // 5 in Python, or 17 / 5 in Java or C when both numbers are integers) and the remainder operator gives the 2 (17 % 5). Together they answer every 'how many full groups, how many left' question, and they always fit back together: 5 × 3 + 2 = 17.

Two kinds of value look like integers and are not. A price such as 9.99 has a fractional part, so it needs a decimal or float type, or you store it as 999 whole cents. And '42' in quotes is a string of two characters; you cannot add to it until you convert it with int('42'). Integers can also have a size limit: in Java an int stops at 2,147,483,647, and adding 1 wraps around to a negative number, while Python's integers simply grow as large as needed.

Think of it like this. Whole eggs in a carton behave like integers: you can have 0, 1 or 12, never 2.5. Sharing 7 eggs between 2 people gives 3 each with 1 left in the carton, exactly what 7 // 2 and 7 % 2 report. The carton picture stops at negative numbers, which integers handle as easily as positive ones.

Core idea. Integers are exact and efficient. Use them for counting and indexing. Use floats/decimals when you need fractions.


When to Use

Choose an integer when a value is a count, a list position or index, a number of steps, or anything that moves in whole units: lives, levels, seats, students per bus. Questions about full groups and leftovers call for integer division and remainder (// and % in Python). If the value can have a fractional part, like a price, a height in metres or an average, use a float or decimal. If the digits are a label rather than a quantity, such as a phone number with a leading 0, store a string.

Tip. Ask: Does the value count or number things in whole steps, so it can never sensibly be 2.5?

How to Recognize It

Before picking a number type, decide whether the quantity comes in whole steps and what should happen when you divide it.

  1. Could the value ever sensibly have a fractional part, such as 3.7 lives or 2.5 players?

    No means an integer fits. Yes, as with a price, a height or an average, points to a float or a decimal type instead.

  2. Is the number used for counting, for a position in a list or for numbering the passes of a loop?

    Yes means an integer, because list positions and range values must be whole numbers. If the digits are only a label, such as a phone number or a zip code, store them as a String.

  3. Does the problem divide one whole number by another and ask for the result?

    Yes means check the language: in Python 7 // 2 gives 3 and 7 % 2 gives the remainder 1, while 7 / 2 gives 3.5. In Java or C, 7 / 2 with two integers gives 3.

  4. Could the value grow past the largest number a fixed-size integer can hold?

    Yes means think about overflow: a 32-bit signed integer stops at 2,147,483,647, and in Java the next step wraps to a negative number. Python integers grow as needed.

Integer vs Float vs String

The same digits can be stored three ways, and the choice decides what arithmetic does to them. Ask whether the value is a whole count, a measurement that needs a decimal point, or a label that only looks like a number.

Integer

Meaning
An integer is a whole number, positive, negative or zero, stored exactly with no decimal point.
Key test
Whole steps only: counts, list positions, lives. Arithmetic on it is exact.
Formula
a=b⋅q+r, 0≤r<b
Example
players = 4, then players = players + 1 gives exactly 5, with no rounding.

Float

Meaning
A float stores a number with a decimal point, such as 9.99 or -0.5, as a close binary approximation.
Key test
Has a decimal point; in Python, / always produces one.
Example
price = 9.99 is a float, and so is 10 / 4, which gives 2.5 even though both inputs were integers.

String

Meaning
A string of digits such as '2024' is text, so it is stored as characters rather than as a number.
Key test
In quotes; + joins instead of adding, and leading zeros are kept.
Example
'12' + '3' gives '123', and a zip code like '02139' keeps its leading zero.

Formula & Notation

Worked Examples

Example 1 — Buses for a field trip

Easy

130 students go on a trip and each bus seats 40. The program runs full = 130 // 40 and left = 130 % 40. Find full and left, and the number of buses needed.
Show the steps and the answer
  1. 130 // 40: 40 fits into 130 three times (40 × 3 = 120), so full = 3.

    Integer division keeps the whole number of groups and drops the fraction.

  2. 130 % 40: 130 - 120 = 10, so left = 10.

    The remainder is what is left after the full groups are taken out.

  3. Check: 40 × 3 + 10 = 130.

    Divisor times quotient plus remainder must rebuild the original number.

  4. left is more than 0, so the trip needs one more bus: 3 + 1 = 4.

    The 10 leftover students still need seats, even though their bus is only partly full.

Answer: full = 3, left = 10, and 4 buses are needed.

Takeaway: Integer division answers 'how many full groups' and the remainder answers 'how many left over'; use both when you need to round up.

Example 2 — When the answer needs a decimal

Standard

Three quiz scores are 7, 8 and 8. A Java program stores them as integers and computes average = (7 + 8 + 8) / 3. What does it get, and how do you get the true average?
Show the steps and the answer
  1. Add the scores: 7 + 8 + 8 = 23.

    A sum of integers is an exact integer.

  2. Divide: in Java, 23 / 3 with two integers gives 7.

    Integer division drops the .666..., and it still gives 7 (as 7.0) even if average is declared as a double, because the division happens first.

  3. Make one operand a decimal: 23 / 3.0 = 7.666..., about 7.67.

    Once either number is a floating-point value, Java does ordinary division. In Python, 23 / 3 already gives 7.666... and 23 // 3 gives 7.

Answer: The integer version gives 7; the true average is about 7.67, using 23 / 3.0.

Takeaway: Counts and totals suit integers, but an average, a rate or a measurement usually needs a decimal, so convert before you divide.

Example 3 — Find the mistake: cents that vanish

Application

A snack costs $2.75 and Ana's program buys 4 of them. She writes price = int(2.75) and total = price * 4, and the program reports a total of $8. Find the mistake and correct it.
Show the steps and the answer
  1. Evaluate int(2.75): converting to an integer drops the fractional part, giving 2.

    An integer cannot hold the .75, and int() cuts it off rather than rounding.

  2. So total = 2 × 4 = 8, while the true cost is 2.75 × 4 = 11.00.

    Losing 75 cents on each snack loses $3 over 4 snacks.

  3. Count in cents, which are whole: price_cents = 275 and total_cents = 275 * 4 = 1100.

    Money measured in its smallest unit is a whole number, so integer arithmetic stays exact.

  4. Split for display: 1100 // 100 = 11 dollars and 1100 % 100 = 0 cents, shown as $11.00.

    Integer division and remainder turn cents back into dollars and cents with no rounding error.

Answer: The total is $11.00, not $8. Store the price as 275 cents (or use a decimal type) instead of truncating it with int().

Takeaway: An integer can hold money only if you count in the smallest whole unit, such as cents; int() on a price throws the cents away.

Where You'll See It

  • Counting players in a game lobby

    The number of players is always a whole number — 0, 1, 2, never 2.5 — so it is stored as an integer, which is exact and never has rounding surprises.

  • Splitting 7 cookies among 2 kids in code

    In Java or C, 7 / 2 with two integers gives 3, and in Python 7 // 2 does the same (the leftover 1 is dropped, or found with the remainder operator, 7 % 2) — this is why a program can surprise you by truncating instead of giving 3.5.

  • A counter that wraps around to a negative number

    Some older games glitched when a score passed the maximum a fixed-size integer could hold and 'rolled over' to a negative value — that is integer overflow.

Common Mistakes

  1. The slip: 7 / 2 always gives 3.5 in code, just like in math.

    Why it's tempting: We learned division gives fractions, so we expect the same from code.

    Instead: When both operands are integers, many languages do integer division and TRUNCATE toward the whole part: 7 / 2 = 3. To get 3.5 you must use a float, e.g. 7.0 / 2.0.

  2. The slip: Integers are best for everything, including money.

    Why it's tempting: Integers are exact, and exactness sounds perfect for currency.

    Instead: Plain integers cannot hold cents or fractions of a dollar directly, and floats can introduce rounding errors. Currency needs a decimal type, or you store whole cents as integers — choosing the wrong type causes money bugs.

  3. The slip: Adding 1 to an integer always gives the next whole number, however big the integer is.

    Why it's tempting: Integer arithmetic is exact, so it seems nothing can go wrong with a simple addition.

    Instead: Fixed-width integers have a ceiling: a Java int stops at 2,147,483,647, and adding 1 wraps around to -2,147,483,648. Use a wider type such as long when values can grow that large; Python's integers grow as needed and never wrap.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. In Python, what are 17 // 5 and 17 % 5?

    Hint: // counts how many whole times 5 fits into 17; % gives what is left.

  2. A bus holds 40 students and 130 students are going on a trip. Using integer division and remainder, how many buses are full, how many students are left over, and how many buses are needed?

    Hint: Use 130 // 40 and 130 % 40, then think about the leftover students.

  3. An 8-bit unsigned integer holds 0 to 255. A score stored this way is 250, and the player earns 10 points. If the value wraps around, what is stored?

    Hint: After 255 the count goes back to 0. There are 256 possible values.

  4. What is the largest value a 16-bit unsigned integer can hold?

    Hint: With n bits there are 2n values, starting at 0.

  5. Which of these should be stored as integers: (a) the number of students in a class, (b) a runner's time such as 12.84 seconds, (c) a page number?

    Hint: Does the value ever need a decimal point?

  6. In Java, with int a = 7 and int b = 2, a student writes a / b and expects 3.5. What is the real result, and how can they get 3.5?

    Hint: What happens to the decimal part when both values are integers?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why does Python give -4 for -7 // 2 and not -3?

Python's // rounds down to the next lower whole number, which is called floor division, and the whole number below -3.5 is -4. Java and C instead drop the fractional part, so -7 / 2 gives -3 there. The remainder follows the same choice: in Python -7 % 2 is 1, because −4×2+1=−7-4 \times 2 + 1 = -7, while Java gives -1. With positive numbers both rules agree, which is why the difference surprises people.

Is there a largest integer?

In math, no. In a computer it depends on the type. A 32-bit signed integer holds values from -2,147,483,648 to 2,147,483,647, and a 64-bit one reaches about 9.2 quintillion. Go past the top in Java and the value wraps around to the most negative number, which is called overflow. Python avoids this by giving each integer as many bits as it needs, so it can work out 21002^{100} exactly, all 31 digits of it.

Why do programmers use % so often?

The remainder answers many everyday questions. n % 2 == 0 tests whether n is even, and position % 4 cycles through 0, 1, 2, 3 again and again, which is how a game takes turns among four players. Paired with //, it splits a number into parts: 135 minutes is 135 // 60 = 2 hours and 135 % 60 = 15 minutes. Clocks, calendars and page numbering all use the same trick.

Is 5.0 an integer?

In math, yes: 5.0 and 5 are the same number. In Python, 5.0 is a float, because the decimal point decides the type, even though the value is whole. The two still compare as equal, so 5 == 5.0 is True. The difference matters when the value is used as a list position: items[5] works, while items[5.0] is an error. Convert with int(5.0) first.

Learning Path

Integer

You are here

Next →

StringBoolean
You need Assignment (storing a value in a variable) and Data Types (every value has a kind) first. Integer adds exact whole-number arithmetic, including integer division and remainder. Next, String covers text, where + joins rather than adds, and Boolean covers the true/false values that comparisons between integers, such as score > 100, produce.

See Also