Sum of First N Natural Numbers
Practice this coding question on Coding Gurukul.
Problem statement
A reading club awards one point on day one, two on day two, and one additional point on each following day. Find the points earned over the entire challenge. Your task Compute the sum of the first n natural numbers, meaning 1 + 2 + ... + n. Important details Every day from one through n contributes once. If n is zero, there are no days and the total is zero. The largest inputs make a day-by-day simulation unnecessary; use the arithmetic-series formula with constant extra space. Intermediate multiplication and the final answer can exceed 32-bit integer limits, so evaluate them using an appropriately wide integer type. Worked example A four-day challenge awards 1 + 2 + 3 + 4 = 10 points. An empty challenge awards 0 points.
Input format
One integer n.
Output format
Print the exact sum.
Constraints
0 <= n <= 1000000000.
Sample input
4
Sample output
10
Sign in to run or submit a solution in C++, Java or Python.