Sum of First N Numbers in Java
Problem
The sum of the first n natural numbers is the running total of every integer from 1 to n, built up one number at a time.
Given a number n, find the sum of every integer from 1 to n.
Java Program
public class SumOfFirstNNumbers {
public static void main(String[] args) {
int n = 10;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
}
}Output
Core Logic
Adding each number from 1 to n into a running total, one at a time, accumulates the full sum by the time the loop finishes.
- 1
sumstarts at0, before any number has been added. - 2
for (int i = 1; i <= n; i++)visits every integer from 1 ton. - 3Each iteration adds the current
iintosumwithsum += i. - 4Once the loop finishes,
sumholds the total of every number visited.
n = 10, the loop adds 1 through 10 one at a time, accumulating to 55.Key Point: sum has to start at 0, not 1 — starting it at the wrong value would silently throw off every total by a fixed amount.
Why: The loop adds one number into the running total per iteration, a count proportional to n, with only a single accumulator kept.
Key Concepts
Approach 2: Using the Sum Formula
public class SumOfFirstNNumbersFormula {
public static void main(String[] args) {
int n = 10;
// Multiply before dividing to keep every intermediate value an exact integer
int sum = n * (n + 1) / 2;
System.out.println("Sum: " + sum);
}
}
Output
Core Logic
The sum of the first n natural numbers has a fixed closed-form formula, which computes the total directly without visiting a single number.
- 1
n * (n + 1) / 2is the standard formula for the sum of an arithmetic series from 1 ton. - 2Multiplying before dividing keeps every intermediate value an exact integer, since one of
nandn + 1is always even. - 3No loop or accumulator is needed — the result comes straight out of the formula.
n = 10, the formula computes 10 * 11 / 2 = 55, the same total the loop finds.Key Point: This formula — often attributed to a young Gauss pairing up numbers from opposite ends of the sequence — turns an O(n) loop into a single O(1) calculation.
Why: The formula computes the result with one multiplication and one division, regardless of how large n is.
Key Concepts
Approach 3: Java 8
import java.util.stream.IntStream;
public class SumOfFirstNNumbersStream {
public static void main(String[] args) {
int n = 10;
int sum = IntStream.rangeClosed(1, n).sum();
System.out.println("Sum: " + sum);
}
}
Output
Core Logic
IntStream.rangeClosed() generates the same 1-to-n sequence the loop walks, and sum() totals it directly, without an accumulator variable or a closed-form formula.
- 1
IntStream.rangeClosed(1, n)produces every integer from 1 ton, inclusive. - 2
.sum()adds up every value the stream produced and returns the total as anint. - 3No accumulator variable is declared or updated by hand — the stream carries the running total internally.
n = 10, the stream produces 1 through 10, and sum() totals them to 55.Key Point: This still visits every number, unlike the formula approach — it trades the formula's O(1) speed for code that reads as 'sum this range' with no arithmetic derivation needed.
Why: sum() still has to visit and add each of the n generated values, the same cost as the manual accumulator loop.