﻿ C++ Examples - Sum of a Range of Values
 Sum of a Range of Values
 This example calculates the sum of values in a range. The program requests two natural numbers from the users
```#include <iostream>
using namespace std;

int main()
{
long Sum = 0;
long First, Last;

cout << "This program allows you to calculate the sum of ";
cout << "numbers\nfrom a lower to a higher values in a range\n";
cout << "Enter the first: ";
cin >> First;
cout << "Enter the last:  ";
cin >> Last;

// Make sure that the last number is higher than the first
// If not, inverse them
if( First > Last )
{
int Temp;

Temp  = First;
First = Last;
Last  = Temp;
}

for( int Counter = First; Counter <= Last; Counter++ )
Sum += Counter;

cout << "\nSum of numbers from " << First << " to " << Last
<< " = " << Sum << endl;

return 0;
}```

Here is an example of running the program:

```This program allows you to calculate the sum of numbers
from a lower to a higher values in a range
Enter the first: 4
Enter the last:  52

Sum of numbers from 4 to 52 = 1372
Press any key to continue```