Java ProgramsControl FlowSum of First N Numbers

Sum of First N Numbers in Java

beginner·  Control Flow  ·  Loops

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.

Input
n = 10
Output
Sum: 55

Java Program

Java
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

Sum: 55

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.

How It Works
  1. 1sum starts at 0, before any number has been added.
  2. 2for (int i = 1; i <= n; i++) visits every integer from 1 to n.
  3. 3Each iteration adds the current i into sum with sum += i.
  4. 4Once the loop finishes, sum holds the total of every number visited.
For 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.

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

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

for loopaccumulator variablerunning sum

Approach 2: Using the Sum Formula

Java
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

Sum: 55

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.

How It Works
  1. 1n * (n + 1) / 2 is the standard formula for the sum of an arithmetic series from 1 to n.
  2. 2Multiplying before dividing keeps every intermediate value an exact integer, since one of n and n + 1 is always even.
  3. 3No loop or accumulator is needed — the result comes straight out of the formula.
For 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.

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

Why: The formula computes the result with one multiplication and one division, regardless of how large n is.

Key Concepts

arithmetic series formulaclosed-form expression

Approach 3: Java 8

Java
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

Sum: 55

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.

How It Works
  1. 1IntStream.rangeClosed(1, n) produces every integer from 1 to n, inclusive.
  2. 2.sum() adds up every value the stream produced and returns the total as an int.
  3. 3No accumulator variable is declared or updated by hand — the stream carries the running total internally.
For 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.

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

Why: sum() still has to visit and add each of the n generated values, the same cost as the manual accumulator loop.

Key Concepts

StreamIntStream.rangeClosed()sum()

Related Programs