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?
Printable View
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?
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();
}
}
Thanks. Like I said I'm new to all this, including the site heh.
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.
Thanks. I really appreciate it. :)
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).
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();
}
}
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.
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:
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.Code:for(int i = 0; i < 10; i++)
//or, same thing done a bit differently
for(int i = 0; i <= 9; i++)