Count Numbers in a Range in Java
Problem
A counting loop visits every number in a range one at a time, incrementing a running total once per visit — the same pattern used to tally anything, not just plain numbers.
Given a lower and upper bound, count how many whole numbers fall within that inclusive range.
Java Program
public class CountNumbersInRange {
public static void main(String[] args) {
int low = 5, high = 15;
int count = 0;
for (int i = low; i <= high; i++) {
count++;
}
System.out.println("Count: " + count);
}
}Output
Core Logic
Stepping through every number from low to high, one at a time, and incrementing a counter at each step tallies the range's size directly.
- 1
countstarts at0, before any numbers have been visited. - 2The loop runs
ifromlowtohigh, inclusive. - 3Each iteration increments
countby exactly1, regardless of whatiactually is. - 4Once the loop finishes,
countholds the total number of values visited.
low = 5 and high = 15, the loop visits 5, 6, 7, ..., 15 — eleven numbers in total.Key Point: This counts by visiting, not by formula — the same loop shape works even if the counting rule were more complex, like only counting even numbers or multiples of 3.
Why: The loop visits every number in the range exactly once, incrementing a single counter variable regardless of how large the range is.
Key Concepts
Approach 2: Using Direct Subtraction
public class CountNumbersInRangeDirect {
public static void main(String[] args) {
int low = 5, high = 15;
// The count of an inclusive range is always high - low + 1
int count = high - low + 1;
System.out.println("Count: " + count);
}
}
Output
Core Logic
The size of an inclusive integer range is always its endpoints' difference plus one, so the count can be computed directly without visiting a single number.
- 1
high - lowgives the number of steps between the two bounds. - 2Adding
1accounts for both endpoints being included in the range. - 3No iteration happens at all — the result comes from a single expression.
low = 5 and high = 15, 15 - 5 + 1 = 11, the same count the loop finds.Key Point: This is the formula the loop version is quietly counting toward — useful to know when only the total is needed, with no reason to visit each number individually.
Why: The count of an inclusive integer range comes from a single subtraction and addition, with no iteration needed at all.
Key Concepts
Approach 3: Java 8
import java.util.stream.IntStream;
public class CountNumbersInRangeStream {
public static void main(String[] args) {
int low = 5, high = 15;
long count = IntStream.rangeClosed(low, high).count();
System.out.println("Count: " + count);
}
}
Output
Core Logic
IntStream.rangeClosed() already generates the exact range, so counting its elements reports the range's size without a hand-written formula or loop.
- 1
IntStream.rangeClosed(low, high)produces every integer in the inclusive range. - 2
.count()returns how many elements the stream produced, as along. - 3No accumulator or explicit arithmetic is written — the stream's own size is the answer.
low = 5 and high = 15, the range stream produces 11 values, so count() returns 11.Key Point: count() returns a long, not an int — a detail that matters if the result is later used somewhere that expects a plain int.
Why: IntStream.rangeClosed() knows its own size from its bounds, so count() can return it directly without generating each element.