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 stored exactly in memory and support arithmetic operations like addition, subtraction, multiplication, and integer division.

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.

Sense of Study hint: When deciding whether to use an integer or a float, ask: does this value ever need a decimal point? If you are counting items, indexing arrays, or tracking scores, use an integer. If you need fractions or precise decimal values, use a float or decimal type.

Worked Examples

Example 1

easy
What is the result of integer division 17รท517 \div 5 and 17modโ€‰โ€‰517 \mod 5? Explain the difference from regular division.

Answer

DIV: 3, MOD: 2. Regular division gives 3.4.

First step

1
Step 1: Regular division: 17รท5=3.417 \div 5 = 3.4.

Full solution

  1. 2
    Step 2: Integer division (floor): 17ย DIVย 5=317 \text{ DIV } 5 = 3 (discard the decimal part).
  2. 3
    Step 3: Modulo: 17ย MODย 5=217 \text{ MOD } 5 = 2 (the remainder after dividing 17 by 5).
Integer division returns the whole-number quotient, while modulo returns the remainder. Together they fully describe the result: 17=5ร—3+217 = 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?

Example 3

medium
Trace: n=256n = 256. Compute n//16n // 16 and nโ€Šmodโ€Š16n \bmod 16.

Example 4

medium
Trace: n=365n = 365. Compute weeks and leftover days as n//7n // 7 and nโ€Šmodโ€Š7n \bmod 7.

Example 5

medium
Trace: count = 0; for i in 1..20: if iโ€Šmodโ€Š3==0i \bmod 3 == 0 then count += 1. What is count?

Example 6

hard
Convert binary 101121011_2 to a decimal integer.

Example 7

hard
Trace a loop: n=13n = 13; while n>0n > 0: print nโ€Šmodโ€Š2n \bmod 2; n=n//2n = n // 2. What sequence is printed?

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.

Example 3

easy
Compute `7 // 2` (integer floor division).

Example 4

easy
Compute `7 % 2` (modulo / remainder).

Example 5

easy
Compute `10 // 3`.

Example 6

easy
Compute `10 % 3`.

Example 7

easy
Is `6` even? Use `6 % 2`.

Example 8

easy
Compute `5 * 4` (integer multiplication).

Example 9

easy
Compute `0 // 5`.

Example 10

easy
Compute `9 % 3`.

Example 11

medium
Trace: `n = 17`. Compute `n // 10` and `n % 10`. What are they?

Example 12

medium
Compute `-7 // 2` in Python (floor division rounds toward negative infinity).

Example 13

medium
Compute `-7 % 2` in Python (result takes the sign of the divisor).

Example 14

medium
Trace: `s = 0`, for `i` in 1,2,3,4 do `s = s + i`. What is `s`?

Example 15

medium
How many times does `3` go into `20`, and what is left over? (`20 // 3`, `20 % 3`)

Example 16

medium
Compute the last digit of `1234` using modulo.

Example 17

medium
A simulated 8-bit unsigned integer holds 0..255. What is `255 + 1` after wrap-around?

Example 18

medium
Use modulo to wrap an index: with array length 5, what is index `7 % 5`?

Example 19

medium
Convert 90 minutes to hours and minutes using `//` and `%` with 60.

Example 20

challenge
Reverse the integer `423` using repeated `% 10` and `// 10`. What is the result?

Example 21

challenge
Using only `// ` and `%` with 10, what is the tens digit of `5764`?

Example 22

challenge
How many trailing zeros does `100!`-style counting need? Simpler: count integers in 1..30 divisible by 5 using `% 5 == 0`.

Example 23

easy
Compute 12โ€Šmodโ€Š512 \bmod 5.

Example 24

easy
Compute 15//415 // 4 (integer division).

Example 25

easy
Compute 8//88 // 8 and 8โ€Šmodโ€Š88 \bmod 8.

Example 26

easy
Compute 1+2+3+41 + 2 + 3 + 4.

Example 27

easy
What is the units digit of 349349 via 349โ€Šmodโ€Š10349 \bmod 10?

Example 28

easy
Compute 100//7100 // 7.

Example 29

medium
A signed 8-bit integer ranges from โˆ’128-128 to 127127. How many distinct values does it represent?

Example 30

medium
Convert 125 seconds to minutes and seconds using integer arithmetic.

Example 31

medium
Compute โˆ’12//5-12 // 5 in Python (floor division).

Example 32

medium
Compute โˆ’12โ€Šmodโ€Š5-12 \bmod 5 in Python.

Example 33

medium
A program uses integers in cents to store dollars. \$3.47 is stored as what integer?

Example 34

medium
How many bits are needed to represent values 0..1023 (unsigned)?

Example 35

hard
A 16-bit unsigned integer overflows when computing 65535+565535 + 5. What value results after wrap-around modulo 2162^{16}?

Example 36

hard
Compute the digit sum of 73827382 using repeated โ€Šmodโ€Š10\bmod 10 and //10// 10.

Example 37

hard
For arrays of length 7, what is the wrap-around index of โˆ’1โ€Šmodโ€Š7-1 \bmod 7 in Python?

Example 38

hard
Why are integer operations typically faster than floating-point on most CPUs?

Example 39

challenge
Use modulo to check if a year is a leap year (simplified Gregorian rule: divisible by 4 but not 100, OR divisible by 400). Is 2100 a leap year?

Example 40

challenge
How many trailing zeros does 10001000 have in base 10, computed using repeated โ€Šmodโ€Š10\bmod 10?

Background Knowledge

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

assignmentdata types