Sum 1..N with i++
Easyjava
Use a counter and the ++ operator in a loop to add up every integer from 1 to N.
Input: one integer N.
Output: one line — the sum 1 + 2 + ... + N.
Example 1
Input
5
Output
15
- 1 <= N <= 1000000
Hint 1
The loop's i++ advances the counter each pass.
Hint 2
Use long for the sum since N can be up to a million.
A standard counting loop: i starts at 1 and i++ moves it forward until it passes N, adding each value to the running sum. long is needed because the sum for N = 1000000 exceeds the int range.