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 input

Input: 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
0
class Solution {
public:
int sumOfFirstN(long long N) {
// Your code goes here
}
};
 
Test Case

Input:

N