Sum of Integers in a Range

Problem

Write a function to compute the sum of integers between two given integer values.

Solution

The naive solution is to loop by going from the lower integer ending at the higher integer while calculating the sum. The other solution is compute the sum using the following formula Sum (1..n) = n (n + 1)/2

Code

Here is the code in C++

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *