Write a function sum_naturals(n) that returns the sum 1 + 2 + ... + n using a for loop and an accumulator variable (do not use the closed-form formula).
Examples
Example 1
Input
sum_naturals(5)
Output
15
Explanation
1+2+3+4+5 = 15.
Example 2
Input
sum_naturals(1)
Output
1
Explanation
Just the single term 1.
Constraints
n is a positive integer.
Must use a loop, not the arithmetic series formula.
Topics
Loopsfor loopaccumulator
Companies
TCSInfosys
Hints
Hint 1
Start a `total = 0` accumulator before the loop.
Hint 2
Loop with `for i in range(1, n + 1):` and add i each time.