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

Thread: for loop sum

  1. #1
    Junior Member
    Join Date
    Feb 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default for loop sum

    hi, im new here!

    I would like to get some help with a little coding. Imagine a for loop in Intellij from 1 to 39. I need the total sum of every number between 1 to 39. Answer should be 780.

    I just cant figure out how to do this, ive searched for 2 days but no succes. Can someone help me with this?

    thanks. This is my first post, if I did something wrong, id like to apoligize also.

    public class Opdracht_4 {
        public static void main ( String[] arg ) {
            for (Integer x = 1; x <= 39; x++) {
     
     
                System.out.println(x);
            }
        }
     
    }
    Last edited by pastakipp; February 12th, 2021 at 01:52 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: for loop sum

    Do you know how to write a for loop with an index that starts at 1 and goes to 39? Start with that.
    Then inside the loop add the values of the for loop index to a variable as the loop is executed.

    Can you post the code you are working with?
    Be sure to wrap all posted code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: for loop sum

    Hello, i have editted my post, i hope i did it good haha!
    This is what i have so far.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: for loop sum

    Ok does that loop do what you expect?

    Next you need to add a statement inside the loop that adds the value of x to a variable so the variable will contain the sum of the values in x.
    Declare the variable outside of the loop so that its value will be available after the loop finishes executing and control goes to the next statement following the loop.

    Note: declare x as a primitive data type: int instead of the class Integer to hold the loop variable's values
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM