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 9 of 9

Thread: for loop that calculates the total of a series of numbers

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default for loop that calculates the total of a series of numbers

    Hi, I am having troubles with creating a for loop that calculates the total of a series of numbers: 1/30 + 2/29 + 3/28 + ... 30/1. I don't know where to start and need some help!


  2. #2
    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: for loop that calculates the total of a series of numbers

    What about that series or programming it don't you understand . . . ?

    Numerator starts at 1 and increases by 1 with each term. Denominator starts at 30 and decreases by 1 with each term. Pretty simple, but maybe it's hard for you to see as a program. What's hard to see?

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

    Default Re: for loop that calculates the total of a series of numbers

    I understand the series, and I understand what is being asked of me. But, I do not understand how to set it up in a for loop. I just learned for loops in my class, and I am not sure if this for loop needs 2 variables, given that there is a changing numerator and a denominator, and how to set up the for loop header.

  4. #4
    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: for loop that calculates the total of a series of numbers

    You need to try and then post the effort with specific questions to get help. We can't write it for you. You could probably do something fancy with just 1 variable in the loop, but make it easier on your brain and use 2.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: for loop that calculates the total of a series of numbers

    This is what I have so far:

     
    for (int x = 1, y = 30; x <= 30, y >= 1; x++, y--)
        {
          int sum = 0;
          sum += (x/y);
        }
     
        System.out.println("The sum of the series of numbers is " + sum);

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: for loop that calculates the total of a series of numbers

    One problem could be the code is using int variables which will do int divisions:
    9/10 = 0
    Change the variable type to double to get better results.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    JessicaCouture95 (November 3rd, 2013)

  8. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: for loop that calculates the total of a series of numbers

    Just revised my code. Does this look good?

    public class Count
    {
     
      public static void main (String[] args) 
     {
        double sum = 0;
     
        for (double x = 1, y = 30; x <= 30 && y >= 1; x++, y--)
        {
        sum += (x/y);
        }
     
        System.out.println("The sum of the series of numbers is " + sum);
     }
    }

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: for loop that calculates the total of a series of numbers

    Does it compile, execute and give the correct answer?
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    JessicaCouture95 (November 3rd, 2013)

  11. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: for loop that calculates the total of a series of numbers

    Just compiled and executed. It looks good, thank you!

Similar Threads

  1. How to add the numbers in the following series?
    By namenamename in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 16th, 2013, 07:23 PM
  2. [SOLVED] How Do I Obtain a Grand Total From My Loop
    By Norm in forum Loops & Control Statements
    Replies: 0
    Last Post: February 4th, 2013, 06:20 PM
  3. Question about for loop to print the sum of series of fractions
    By _K_ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 08:08 AM
  4. A For loop that calculates every possible outcome for 2 AND 3 chars...
    By MyNamesMatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2012, 10:59 AM
  5. Generate prime numbers within fiboncacci series?
    By Manish87 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 7th, 2010, 12:24 PM

Tags for this Thread