Integer Examples: 47 Problems with Answers
Worked examples of Integer to try before you open each solution, then practice problems with answers.
Worked Examples
Example 1
easyShow solution
Answer
First step
Full solution
- 2. Step 2: Integer division (floor): (discard the decimal part).
- 3. Step 3: Modulo: (the remainder after dividing 17 by 5).
Example 2
mediumExample 3
mediumExample 4
mediumExample 5
mediumExample 6
hardExample 7
hardPractice Problems
Try each one, then open the solution to compare your method.
Example 1
easyExample 2
mediumExample 3
easyExample 4
easyExample 5
easyExample 6
easyExample 7
easyExample 8
easyExample 9
easyExample 10
easyExample 11
mediumExample 12
mediumExample 13
mediumExample 14
mediumExample 15
mediumExample 16
mediumExample 17
mediumExample 18
mediumExample 19
mediumExample 20
challengeExample 21
challengeExample 22
challengeExample 23
easyExample 24
easyExample 25
easyExample 26
easyExample 27
easyExample 28
easyExample 29
mediumExample 30
mediumExample 31
mediumExample 32
mediumExample 33
mediumExample 34
mediumExample 35
hardExample 36
hardExample 37
hardExample 38
hardExample 39
challengeExample 40
challengeWhat 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.
Ask yourself: Does the value count or number things in whole steps, so it can never sensibly be 2.5?
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.
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.
Read the full lesson on IntegerRelated concepts
Background knowledge
These ideas may be useful before you work through the harder examples.