# Summation Problem

• 02-04-2010, 03:32 AM
a7x75
Summation Problem
I have to write a program that inputs an integer, n, from the command line and
displays the string “1 + 2 + 3 + 4 + ... + n = sum.”

I'm still new to this so I don't even know how to start it. Any help?
• 02-04-2010, 03:47 AM
a7x75
This is my program so far:

Code:

```public class Summation {         public static void main(String [] args)         {                 int n = Integer.parseInt(args[0]);                 while (n >= 1)                 {                         System.out.print(n + " ");                         n++;                 }                 System.out.println();         } }```
• 02-04-2010, 04:01 AM
a7x75
Thanks. Like I said I'm new to all this, including the site heh.
• 02-04-2010, 04:03 AM
Fubarable
OK, so you actually do have a start, and that is a good thing. I recommend that you consider using another loop, since while loops are best used in situations where you don't know in advance how many times to loop. Here you DO know how many times before the loop begins -- "n" times exactly. So which type of loop would be better here?

Next you'll want to sum the numbers as the loop loops. To do this, create an int variable before your loop, and add the number created in the loop to this summation variable. After the loop is done, it will hold your sum of interest.

So if you understand my recommendations, please give it another try. If not, then please ask what confuses you.

Much luck and welcome to the forum. Oh, I've added code tags to your post to make the code retain its formatting. To learn how to do this yourself, please click on the link in my signature below.
• 02-04-2010, 04:05 AM
a7x75
Thanks. I really appreciate it. :)
• 02-04-2010, 04:08 AM
Fubarable
No problem. You understand that your while loop above will start printing n and ever larger numbers and will never end since n will always be bigger than 1 (OK, it will end when int overflow occurs).
• 02-04-2010, 04:42 AM
a7x75
I think I got the "for" loop part down, but I'm still unsure of what to do with integer n:

Code:

``` public class Summation {         public static void main(String [] args)         {                 int sum = 0;                 int n = n;                 for (n >= 1, n = n + 1);                 {                         System.out.print(sum = + '=' + n);                         n++;                 }                 System.out.println();         } }```
• 02-04-2010, 05:10 AM
Fubarable
Not quite. In your for loop, don't use n as the index. Instead use a separate index variable (by custom, it us usually "i"). Use n instead for the for loop's end condition. Then print out i and add i to the summation variable within the for loop.
• 02-05-2010, 06:38 AM
m00nchile
Just a quick pointer, the structure of a for loop is as follows:
for(counter;end-condition;increment).
counter is usually an int value, and here is where you initialise it, eg. set it's start value, for example int i = 0.
The end condition defines when the loop stops, well, looping :).
The incremet defines what happens to the counter after each time the loop executes, most commonly i++ is used (i++ means i=i+1).
So, an example of a for loop that executes 10 times would be:
Code:

```for(int i = 0; i < 10; i++) //or, same thing done a bit differently for(int i = 0; i <= 9; i++)```
So, our counter (int i) has the value 0 on the first loop iteration, it checks the end contition (0 < 10 is true, so the loop can execute), does whatever the code within the loop tells it to, and at the end, increments the counter (i++), and starts again.