848. Sum of First N Natural Numbers
You are given an integer N. Your task is to calculate the sum of the first N natural numbers, i.e., the sum from 1 to N.
Return the result as an integer.
Example 1:
Input:Â N = 5
Output:Â 15
Explanation:Â 1 + 2 + 3 + 4 + 5 = 15
Example 2:
Input: N = 6
Output: 21
Explanation: 1 + 2 + 3 + 4 + 5 + 6 = 21
Now Your Turn!
Pick the correct output for the given inputInput: N = 10
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ N ≤ 4×104
- The result of the sum will not exceed 109