Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1 Filename: sum.cpp Prompt the user and let them enter two integers (we

ID: 3902599 • Letter: E

Question

Exercise 1

Filename: sum.cpp

Prompt the user and let them enter two integers (we'll call them x and y, for the purposes of this writeup). Use a loop to compute and print the sum of the ingeters in the range x through y (including the endpoints). Output should look like the sample runs below, and print the addition details from the smallest to the largest number.

(Note: The user input will not necessarily have the lowest number first -- see Sample Run 2. You must take this into account).

Sample Runs

(user input is underlined, to distinguish it from output)

Sample Run 1

Sample run 2

Sample run 3

Explanation / Answer

#include <iostream>

using namespace std;

int main() {

// declaring variables

int a, b;

int high, low, sum;

// taking user input for 2 numbers

cout << "Input two integers: ";

cin >> a >> b;

// knowing what is high and what is low

if(a > b)

{

high = a;

low = b;

}

else

{

low = a;

high = b;

}

// printing first line and first number in sequence

cout << endl << "Sum of values from " <<low <<" through " << high <<" is:" << endl << low;

sum = low++;

// looping through remaining numbers and adding to sum

for(low; low<=high; low++)

{

cout << " + " << low;

sum += low;

}

// printing sum

cout << endl << "= " << sum;

}

/*SAMPLE OUTPUT

Input two integers: 10 20

Sum of values from 10 through 20 is:

10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20

= 165

Input two integers: 9 -4

Sum of values from -4 through 9 is:

-4 + -3 + -2 + -1 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9

= 35

Input two integers: 7 7

Sum of values from 7 through 7 is:

7

= 7

*/