Problem with adding sum of a range

My goal is to have the user enter a number above 0. My program should then sum every number from 0 to the number that they entered. For example, if the user entered a 5, my program should add (0 + 1 + 2 + 3 + 4 + 5) and then produce the sum of 15. Here is what I have so far.

#include <iostream>
using namespace std;

int main ()
{
int counter; // Loop-control variable
int sum; // Running sum
int digit;

cout << "Enter a one-digit number; press return."
<< endl;
cin >> digit;
counter = 0;
sum = 0;

while (digit >= counter)
{
sum = digit + counter;
counter++;
}
cout << "Sum of digits between 0 and "
<< digit << " is " << sum << endl;
system("pause");
return 0;
}
And your question is?

PS: You may as well start counter at 1. x+0=x.
Last edited on
Topic archived. No new replies allowed.