### Using a loop to get sum of integers? Help

I did some more research and found a way to solve for the sum thanks!
 123456789101112131415161718192021222324252627282930 #include //For cout and cin #include //for getch #include //display output more neatly/organized using namespace std; int main() { //Variable int positiveInteger; //number input from user int num = 1; //num starts with 1 int sum; // Sum of all integers from 1 to number //Ask user for a positive integer cout << "Please enter a positive integer value: " << endl; cin >> positiveInteger; //Formula for sum calcuation sum = positiveInteger * (positiveInteger +1) / 2; //Find sum of all the number while (positiveInteger <= sum || positiveInteger > 0) { cout << "" << endl; num++; cout << } _getch(); // hold the screen }
Last edited on
You don't need to use the formula (except for purposes of checking the output).

First, set the total equal to zero.
The loop condition should be:
 while (num <= positiveInteger)

Inside the loop, add the current value of num to the total, then add 1 to num.
After the loop ends, print out the total.
Last edited on
Topic archived. No new replies allowed.