Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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?

2. ## 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

3. ## 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:
```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.

4. ## 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....

5. ## 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:
*
**
***
****