Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with a Summing Series please? :)

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with a Summing Series please? :)

    Problem: Write a program to sum the following series:
    1 + (1 + 2) + (1+ 2 + 3) + (1 + 2 + 3 + .........+ n)

    Hey guys just stuck on a problem here. So far I have:

    public static void main(String[] args) {



    int sum = 1;

    System.out.println("input number of times for series to run:");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();



    for (int i = 0; i <= n;i++){


    sum = sum + (i+n);
    System.out.println(sum);
    }
    }
    }

    But the output answers is always wrong. Ive tried using the += operator and no joy. And also had before this sum = i + (i + n);

    could ye tell me what I'm doing wrong?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need help with a Summing Series please? :)

    Look at the output... Determine how the program come up with such output... Look at the code again closely to see what math happens along the way.
    Understanding why the output is what it is will be the key to correcting it

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with a Summing Series please? :)

    It's always helpful to post a sample run and use it to illustrate what you consider "wrong." For example, my sample run of your code is:
    input number of times for series to run:
    5
    6
    12
    19
    27
    36
    46
    And I suppose you're wondering why the series isn't:

    1
    3
    6
    10
    15

    Is that right?

    There are a number of reasons, but you should wonder why your series is growing so quickly. Are you adding the right value each time through the loop? Work through the loop a few times on paper to see what's going on.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a Summing Series please? :)

    Yes its the output alright. I'll work through it a bit on paper and see what I come up with. I know its staruing me in the face, I cant get my head around it! haha thanks guys

    --- Update ---

    i think i have it:

    for (int i = 0; i <= n; i++){

    sum = sum +i;

    }

    have to wait to get to my own comp to run it!

    --- Update ---

    wait... nevermind....

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Need help with a Summing Series please? :)

    Getting a little closer to what you are suppose to do. Do you understand what is happening through each iteration of the loop? Write out both your first loop and the second one on paper and find out what results you are getting and why you are getting that result. In your class has the teacher made you do the triangle exercise?

    This:
    *
    **
    ***
    ****

Similar Threads

  1. [SOLVED] HELP - Summing Numbers, with and without Input
    By SunnyS in forum Loops & Control Statements
    Replies: 3
    Last Post: October 18th, 2012, 04:01 PM
  2. Summing n terms of a Fibonacci Sequence
    By wayfaring_stranger in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2012, 12:45 AM
  3. Why is it summing a long negative integer to zero????
    By PearlLV88 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 30th, 2011, 05:50 AM
  4. Summing a 4X4 Matrix in JOptionPane
    By Java Neil in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 15th, 2011, 09:15 AM
  5. Im so confused! Counting and Summing
    By captiancd89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 28th, 2010, 06:49 PM

Tags for this Thread