#CCBCHMOT0000007. Tổng các số từ 1 đến n bằng while (Sum One Through N with While)
Tổng các số từ 1 đến n bằng while (Sum One Through N with While)
Sum One Through N with While
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given nonnegative n, compute 1+2+...+n using while with a separate loop counter and sum accumulator. The empty sum for n=0 is zero.
Input
One integer n.
Output
Print the integer sum from 1 to n.
Subtasks
- Subtask 1 (20%): 0 <= n <= 5.
- Subtask 2 (30%): 0 <= n <= 1000.
- Subtask 3 (50%): 0 <= n <= 100000.
Examples
Example 1
Input
4
Output
10
Explanation
Adding 1,2,3,4 to the initial zero yields 10.
Example 2
Input
0
Output
0
Explanation
With n=0, the loop does not execute.