Using a loop to get sum of integers? Help

I did some more research and found a way to solve for the sum thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>	//For cout and cin
#include <conio.h>	//for getch
#include <iomanip>	//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.