Results 1 to 9 of 9
  1. #1
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default 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. #2
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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. #3
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks. Like I said I'm new to all this, including the site heh.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #5
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks. I really appreciate it. :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #7
    a7x75 is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

Similar Threads

  1. Fibonacci summation problem
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 12-01-2009, 07:28 PM
  2. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  3. (Help) Quotient summation with prime numbers
    By SapphireSpark in forum New To Java
    Replies: 27
    Last Post: 10-24-2008, 09:28 AM
  4. (Help) Fraction Summation and Exponents
    By SapphireSpark in forum New To Java
    Replies: 19
    Last Post: 10-09-2008, 05:01 AM

Posting Permissions

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