Back to Stream Creation & Utilities
Q18Question 18 of 18

Find the sum of all even numbers from 1 to 100 using IntStream.rangeClosed

Key Concepts
  • IntStream.rangeClosed(1, 100) produces the inclusive range 1 through 100 as a primitive IntStream
  • filter(n -> n % 2 == 0) keeps only even numbers; sum() totals them to 2550
  • rangeClosed is inclusive on both ends; range excludes the upper bound
  • Primitive streams avoid boxing overhead, making them efficient for numeric ranges
  • For large ranges, use asLongStream() before sum() to guard against int overflow
  • The filter-free alternative rangeClosed(1, 50).map(i -> i * 2).sum() generates even numbers directly

Definition

IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0).sum() generates the range, keeps even numbers, and totals them to 2550.
  • IntStream.rangeClosed(1, 100) produces 1 through 100 inclusive as a primitive IntStream.
  • filter(n -> n % 2 == 0) keeps numbers divisible by 2 with no remainder.
  • sum() totals the survivors into a plain int.
  • No boxing because the entire pipeline stays in the primitive domain.
  • rangeClosed vs range: rangeClosed includes the upper bound; range excludes it.

Priya at the National Bank of India lists voucher numbers 1 through 100, keeps only the even-numbered ones, and adds them up for a batch control total.

Understanding the Concepts

What does this question ask?

The interviewer wants you to sum even numbers in a range using IntStream. This tests rangeClosed, filter with modulo, and the sum terminal operation.

How do you sum even numbers from 1 to 100?

JAVA
package com.bank.core;

import java.util.stream.IntStream;

public class SumEvenNumbers {
    public static void main(String[] args) {
        int total = IntStream.rangeClosed(1, 100)
            .filter(n -> n % 2 == 0)
            .sum();

        System.out.println("Sum of even numbers 1-100: " + total);
    }
}
TEXT
Sum of even numbers 1-100: 2550

How is rangeClosed different from range?

| Method | Bounds | Example |
|---|---|---|
| rangeClosed(1, 100) | Inclusive both ends | 1, 2, 3, ... 99, 100 |
| range(1, 100) | Start inclusive, end exclusive | 1, 2, 3, ... 98, 99 |

JAVA
System.out.println(IntStream.rangeClosed(1, 5).sum());  // 15 (1+2+3+4+5)
System.out.println(IntStream.range(1, 5).sum());        // 10 (1+2+3+4)

Use rangeClosed when the upper bound should be included.

What is the filter-free alternative?

JAVA
// Generate even numbers directly without filtering
int total = IntStream.rangeClosed(1, 50)
    .map(i -> i * 2)
    .sum();

System.out.println("Sum: " + total);  // 2550

This generates 2, 4, 6, ... 100 directly by mapping each index to twice itself, skipping odd numbers entirely.

How do you guard against overflow on large ranges?

JAVA
// For large ranges, int sum could overflow
long safeSum = IntStream.rangeClosed(1, 1_000_000)
    .filter(n -> n % 2 == 0)
    .asLongStream()    // widen to long before summing
    .sum();

System.out.println("Large sum: " + safeSum);
TEXT
Large sum: 250000500000

Without asLongStream, the int sum would silently overflow and produce a wrong result.

How is this different from a for loop?

JAVA
// Imperative for-loop
int loopSum = 0;
for (int i = 2; i <= 100; i += 2) {
    loopSum += i;
}

// Declarative stream
int streamSum = IntStream.rangeClosed(1, 100)
    .filter(n -> n % 2 == 0)
    .sum();

// Both produce 2550

The stream version reads declaratively and states intent clearly. The loop version steps by 2, which is slightly more efficient but less readable.

What are common mistakes?

Mistake: using range instead of rangeClosed and missing the upper bound.

JAVA
// WRONG: range(1, 100) excludes 100
int wrong = IntStream.range(1, 100)
    .filter(n -> n % 2 == 0)
    .sum();
// 2450 instead of 2550 (missing 100)

// CORRECT: rangeClosed includes 100
int correct = IntStream.rangeClosed(1, 100)
    .filter(n -> n % 2 == 0)
    .sum();
// 2550

Mistake: using boxed() unnecessarily.

JAVA
// WRONG: boxing adds overhead for no reason
int wrong = IntStream.rangeClosed(1, 100)
    .boxed()
    .filter(n -> n % 2 == 0)
    .mapToInt(Integer::intValue)
    .sum();

// CORRECT: stay in the primitive domain
int correct = IntStream.rangeClosed(1, 100)
    .filter(n -> n % 2 == 0)
    .sum();

What about summing odd numbers?

JAVA
int oddSum = IntStream.rangeClosed(1, 100)
    .filter(n -> n % 2 != 0)
    .sum();

System.out.println("Odd sum: " + oddSum);  // 2500

What are common interview follow-up questions?

  • "What is the mathematical formula?" Sum of evens 1 to n: n/2 * (n/2 + 1), so 50 * 51 = 2550.
  • "How do you sum numbers divisible by 3?" Change the filter to n % 3 == 0.
  • "How do you compute this in parallel?" Use .parallel() before sum().

Visual Overview

DIAGRAM
  IntStream.rangeClosed(1, 100)
  → [1, 2, 3, 4, 5, ... 98, 99, 100]
       │
       │  .filter(n -> n % 2 == 0)
       ▼
  [2, 4, 6, 8, 10, ... 96, 98, 100]  (50 numbers)
       │
       │  .sum()
       ▼
  2550

  Alternative: rangeClosed(1, 50).map(i -> i * 2)
  → [2, 4, 6, 8, ... 100]  → sum → 2550

Key Takeaway

Summing even numbers from 1 to 100 uses IntStream.rangeClosed for the inclusive range, filter with modulo to keep even numbers, and sum to total them. The entire pipeline stays in the primitive int domain with no boxing overhead.

rangeClosed is the right choice because 100 must be included. For large ranges, widen with asLongStream before summing to guard against int overflow. The filter-free alternative generates even numbers directly by mapping each index to twice itself.

At the National Bank of India, Rajesh used this shape in a control-figure check that totals even-numbered vouchers to verify a batch.

Remember one sentence: IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0).sum() is 2550, and use asLongStream for large ranges to prevent overflow.

---

Share
Use ← → arrow keys to navigate