Range between numbers

#TC1017 #WSQ07

This problem was actually a little more difficult to solve than the previous ones. Although it does what it needs to do, I think I made things somewhat more complicated than they needed to be, which is why I added some comments to remind myself what I did and why.

The programm begins by asking the user for two numbers, for which it will calculate the total sum of the numbers inbetween, inclusive. First of all it checks that the numbers given are in the proper order: lower to higher, then I use the difference between those numbers to calculate the amount of sums that have to be done and add a -1 so the difference matches the quantity of numbers between them properly.

Once that has been done, the programm then sums the numbers n times using a while loop until the counter (n) matches the quantity of numbers between the ranges.

As I said, a little complicated but it works!


Source Code:

#include <iostream>
using namespace std;

int main(){

int lower, higher;
int difference, sum;
int counter;
int loweract;

cout <<“Please enter the lower bound:” << endl;
cin >> lower;
cout << “Please enter the higher bound:” << endl;
cin >> higher;

while (lower > higher) {

cout << endl;

cout << “Please correct the order of your bounds!” << endl;

cout <<“Please enter the lower bound:” << endl;
cin >> lower;
cout << “Please enter the higher bound:” << endl;
cin >> higher;


do { //could this be done with a for loop?

difference = ((higher – lower)-1); // gives the number of integer sums aside from the bounds
counter ++; // counter is needed so that the sum stops once the counter reaches the number of sums
loweract = lower + counter

loweract; // loweract gets added to loweract plus the bounds with the purpose of storing its value for the total sum

sum = lower + loweract + higher;

} while (difference != counter);

cout << “The sum of integers in the range you provided is:” << endl;
cout << sum << endl;



Photo Credit: <a href=”https://www.flickr.com/photos/50318388@N00/23640476465/”>mag3737</a&gt; via <a href=”http://compfight.com”>Compfight</a&gt; <a href=”https://creativecommons.org/licenses/by-nc-sa/2.0/”>cc</a&gt;

CC BY-SA 4.0 Range between numbers by diegodamy is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.