Need help with a Summing Series please? :)

Problem: Write a program to sum the following series:

1 + (1 + 2) + (1+ 2 + 3) + (1 + 2 + 3 + .........+ n)

Hey guys just stuck on a problem here. So far I have:

public static void main(String[] args) {

int sum = 1;

System.out.println("input number of times for series to run:");

Scanner scan = new Scanner(System.in);

int n = scan.nextInt();

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

sum = sum + (i+n);

System.out.println(sum);

}

}

}

But the output answers is always wrong. Ive tried using the += operator and no joy. And also had before this sum = i + (i + n);

could ye tell me what I'm doing wrong? :)

Re: Need help with a Summing Series please? :)

Look at the output... Determine how the program come up with such output... Look at the code again closely to see what math happens along the way.

Understanding why the output is what it is will be the key to correcting it

Re: Need help with a Summing Series please? :)

It's always helpful to post a sample run and use it to illustrate what you consider "wrong." For example, my sample run of your code is:

Code :

input number of times for series to run:
5
6
12
19
27
36
46

And I suppose you're wondering why the series isn't:

1

3

6

10

15

Is that right?

There are a number of reasons, but you should wonder why your series is growing so quickly. Are you adding the right value each time through the loop? Work through the loop a few times on paper to see what's going on.

Re: Need help with a Summing Series please? :)

Yes its the output alright. I'll work through it a bit on paper and see what I come up with. I know its staruing me in the face, I cant get my head around it! haha :) thanks guys

--- Update ---

i think i have it:

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

sum = sum +i;

}

have to wait to get to my own comp to run it! :)

--- Update ---

wait... nevermind....

Re: Need help with a Summing Series please? :)

Getting a little closer to what you are suppose to do. Do you understand what is happening through each iteration of the loop? Write out both your first loop and the second one on paper and find out what results you are getting and why you are getting that result. In your class has the teacher made you do the triangle exercise?

This:

*

**

***

****