### Calculating the sum with for loop

I don't know what to do after this...
Can you help me little bit?

 ``12345678`` `````` int n, i, s; cout << "How much do you want to count the sum of?\n"; cin >> n; for(i=0; i> sum; //WHAT A HECK NOW?!?! }``````

If you enter n = 3
I want to calculate sum (10, 15, 20 and to print 10 + 15 + 20 = 45)

If you enter n = 4
I want to calculate sum (10, 12, 20, 39 and to print 10 + 12 + 20 + 39= 81 I think?) .. and so on.
Last edited on
You did not specify if numbers are natural so I am assuming they are natural.
Also I recommend you to declare the variable i inside the loop like this:

` for (int i = 0; i < n; i++) `

 ``1234567891011121314`` ``````#include using namespace std; int main () { int n, i, x, sum; cout << "How much do you want to count the sum of?\n"; cin >> n; for(i = 0; i < n; i++) { cin >> x; sum += x; if (i+1 != n) cout << x << " + "; else cout << x << " = " << sum << endl; } }``````

Last edited on
ok @Oriol Serrabassa

i put sum = 0 and it worked
with sum;
the result of 12 + 12 it was 2868.. something
thank u, u helped me a lot! :D
Last edited on
Topic archived. No new replies allowed.