We focus on the elementary form of the summation operator: $$\sum_{n=a}^{b} n$$, which adds every integer from a lower bound a to an upper bound b, inclusive. Example (from your graphic): $$\sum_{n=1}^{4} n = 1+2+3+4 = 10.$$
Users provide just two integers—a and b—and the programme returns their sum. Internally we exploit the Gauss closed‑form formula $$S = \frac{(a+b)(b-a+1)}{2}$$ for constant‑time performance.
a.b.a ≤ b; otherwise it shall issue an error and terminate.<value>” on a new line.b‑a ≤ 109.Compile with javac SimpleSigmaCalculator.java and run via java SimpleSigmaCalculator.
import java.util.Scanner;
/**
* CLI calculator for Σ_{n=a}^{b} n using the Gauss formula.
* Author: <Your Name> – SOEN 6011 D1
*/
public final class SimpleSigmaCalculator {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Lower bound a (integer): ");
if (!sc.hasNextLong()) { System.out.println("Invalid integer."); return; }
long a = sc.nextLong();
System.out.print("Upper bound b (integer): ");
if (!sc.hasNextLong()) { System.out.println("Invalid integer."); return; }
long b = sc.nextLong();
if (a > b) { System.out.println("Error: a must not exceed b."); return; }
long count = b - a + 1;
// Gauss formula (a+b)*count/2, evaluate in long (may overflow if sum > 9e18)
long sum = (a + b) * count / 2;
System.out.printf("Σ = %d%n", sum);
}
}
private SimpleSigmaCalculator() { }
}