Java ProgramsControl FlowCount Numbers in a Range

Count Numbers in a Range in Java

beginner·  Control Flow  ·  Loops

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.

Input
low = 5, high = 15
Output
Count: 11

Java Program

Java
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

Count: 11

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.

How It Works
  1. 1count starts at 0, before any numbers have been visited.
  2. 2The loop runs i from low to high, inclusive.
  3. 3Each iteration increments count by exactly 1, regardless of what i actually is.
  4. 4Once the loop finishes, count holds the total number of values visited.
For 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.

Complexity
Time Complexity: O(range)Space Complexity: O(1)

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

for loopcounting patternrunning total

Approach 2: Using Direct Subtraction

Java
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

Count: 11

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.

How It Works
  1. 1high - low gives the number of steps between the two bounds.
  2. 2Adding 1 accounts for both endpoints being included in the range.
  3. 3No iteration happens at all — the result comes from a single expression.
For 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.

Complexity
Time Complexity: O(1)Space Complexity: O(1)

Why: The count of an inclusive integer range comes from a single subtraction and addition, with no iteration needed at all.

Key Concepts

arithmetic formulaconstant time

Approach 3: Java 8

Java
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

Count: 11

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.

How It Works
  1. 1IntStream.rangeClosed(low, high) produces every integer in the inclusive range.
  2. 2.count() returns how many elements the stream produced, as a long.
  3. 3No accumulator or explicit arithmetic is written — the stream's own size is the answer.
For 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.

Complexity
Time Complexity: O(1)Space Complexity: O(1)

Why: IntStream.rangeClosed() knows its own size from its bounds, so count() can return it directly without generating each element.

Key Concepts

StreamIntStream.rangeClosed()count()

Related Programs