Integer Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Integer.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

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

Integers are counting numbers in code โ€” things you can count without fractions: age, score, quantity, index.

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

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

Common stuck point: Integer division truncates in many languages: 7 / 2 gives 3, not 3.5. This catches many beginners by surprise.

Worked Examples

Example 1

easy
What is the result of integer division 17 \div 5 and 17 \mod 5? Explain the difference from regular division.

Solution

  1. 1
    Step 1: Regular division: 17 \div 5 = 3.4.
  2. 2
    Step 2: Integer division (floor): 17 \text{ DIV } 5 = 3 (discard the decimal part).
  3. 3
    Step 3: Modulo: 17 \text{ MOD } 5 = 2 (the remainder after dividing 17 by 5).

Answer

DIV: 3, MOD: 2. Regular division gives 3.4.
Integer division returns the whole-number quotient, while modulo returns the remainder. Together they fully describe the result: 17 = 5 \times 3 + 2.

Example 2

medium
A program stores a student's age as an integer. Why would an integer be more appropriate than a floating-point number for age in years?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
What are the results of: (a) 25 MOD 7, (b) 100 DIV 3, (c) 8 MOD 2?

Example 2

medium
A vending machine program stores the price as 175 (meaning $1.75 in cents). A customer inserts 200 cents. Using only integer arithmetic (DIV for integer division, MOD for remainder), calculate the change in dollars and remaining cents.

Background Knowledge

These ideas may be useful before you work through the harder examples.

assignmentdata types