### I need help online now have no clue at all how to do this

Write a program that asks the user for a number. Then the program outputs the sum of all positive numbers up to that number.

For example, if the user types in 10
The program outputs 55
Because that is what 1+2+3+4+5+6+7+8+9+10 is.

int main(){
int number, result=0;

cin>>number;
for(int i=0;i<number;i++){
result= result+i;
}

}

something like that, figure out the rest, this program will not include the number 10 in the addition, it goes UP TO number.
Use below code
Last edited on
Why making the operation so complicated?
Sorry mrtyson86. Use this:

 ``123456789101112131415`` ``````#include "stdafx.h" #include using namespace std; int main() { int input = 0; int newNum = 1; cout << "Enter number: "; cin >> input; for (int i=1; i < input; i++) { newNum += i + 1; } cout << newNum; }``````

or something to that effect.
Last edited on
Or you can use early German mathematician Carl Gauss's formula for finding the sum of all numbers up to N, and eliminate the need for the loop all together:

Gauss's formula is:
 `` `` ``x = n * (n+1) / 2``

Less complex yet:
 ``1234567891011`` ``````#include "stdafx.h" #include using namespace std; int main() { int input, newNum; cout << "Enter number: "; cin >> input; newNum = input * (input + 1)/2; cout << newNum; }``````

I guess math really is cool?
Last edited on
What I would do is to use the formula as a cross-check in order to verify that the loop and summation is giving the correct answer.

Topic archived. No new replies allowed.