Sums

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theinsider2012
    New Member
    • Nov 2007
    • 2

    #1

    Sums

    JAVA for Windows XP

    the question is:

    Write an application that shows the sum of 1 to n for every n from 1 to 50. That is, the program prints 1 (the sum of 1 alone), 3 (the sum of 1 and 2), 6 (the sum of 1, 2, and 3), and so on.

    I'm not looking for a answer, just some ideas on where to start. I was able to create an application that sums the integers from 1 to 50 and displays the answer 1275, but I'm not sure how to go about writing an app that shows the sum for every n.

    thanks for any help.
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    please provide the section of code you are having trouble with.
    Last edited by epots9; Nov 6 '07, 09:09 PM. Reason: jumped the gun

    Comment

    • theinsider2012
      New Member
      • Nov 2007
      • 2

      #3
      Well like I said, the only code I have is for the app that sums from 1 to 50. So here it is.

      Code:
      public class Sum50
      {
      	public static void main(String[] args)
      	{
      		int start = 0;
      		int sum = 0;
      		
      		while (start < 50)
      		{
      			++start;
      			sum = sum + start;
      		}
      		System.out.println("The sum of 1 to 50 is " + sum);
      	}
      }

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        To print out every partial sum, simply add a print statement in your while loop. Think about it what needs to be printed, and if you're still having trouble, post again and we'll help you.

        Comment

        Working...