1. Member
Join Date
Feb 2010
Posts
10
Rep Power
0

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

2. Member
Join Date
Feb 2010
Posts
10
Rep Power
0
This is my program so far:

Java 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();
}
}```
Last edited by Fubarable; 02-04-2010 at 03:59 AM. Reason: Code tags added

3. Member
Join Date
Feb 2010
Posts
10
Rep Power
0
Thanks. Like I said I'm new to all this, including the site heh.

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

5. Member
Join Date
Feb 2010
Posts
10
Rep Power
0
Thanks. I really appreciate it. :)

6. 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).

7. Member
Join Date
Feb 2010
Posts
10
Rep Power
0
I think I got the "for" loop part down, but I'm still unsure of what to do with integer n:

Java 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();
}
}```

8. 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.

9. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
8
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:
Java 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•