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

Thread: Loop Assignment

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop Assignment

    Hi,
    I have an assignment to do, I've been trying to do it, but I just can't find the right solution..

    - What is it about?
    - About an algorithm 1+2+3+4+...+n=t
    (e.g.)
    Input: 3, Outcome: 1+2+3=6
    Input: 4,Outcome: 1+2+3+4=10
    or
    Input:10, Outcome: 1+2+3+4+5+6+7+8+9+10=55
    It should only read numbers from 1 up to 999.
    It should also contain code:
    int i,n,s;
            n = //Input;
            i = 1;
            s = 1;
            System.out.println(1);
            while( i!=n ) {
    // WRITE YOUR CODE HERE!
    }

    I am very, very new to java..
    Could you either help me to do it or give me a hint?


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Loop Assignment

    Do you have any idea how you would do it by hand? On a piece of paper perhaps?

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Assignment

    I took 3 java classes so far, so I don't know it yet.

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

    Default Re: Loop Assignment

    The question was about what the technique was for solving the problem using paper and pencil. It wasn't about writing a java program.
    Can you make a list of the steps that need to be done to solve the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Assignment

    I am not sure if this is what you mean, but these are the conditions that must be met.


    1 <= i <= n
    i = 1+2+3+4+...+n
    s = sum of i


    I'm done with the exercise and this is my outcome:
      public static void main( String[] args )
        {
            int i,n,s;
            n = 10; //10 is only as an example
            i = 1;
            s = 1;
            System.out.print(1);
            while( i!=n )
            {
                s += i;
                i++;
                System.out.print("+" + i);
                }
            i = n%100;
            s = s+i;
            System.out.print("=" + (s-1) );
            System.out.println();

    Can You tell me if I could've done something easier? Or give me hints&tips?
    Last edited by yellowglow; September 3rd, 2014 at 05:37 PM.

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

    Default Re: Loop Assignment

    Can you post some of the program's output to show what it generates?

    Also some comments describing what the code does and why.
    For example, what does this do?
           i = n%100;
           s = s+i;
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Assignment

    This is the output: output.PNG
    Is my code so messed up?
    Last edited by yellowglow; September 3rd, 2014 at 06:13 PM.

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

    Default Re: Loop Assignment

    Is my code so messed up?
    What does that mean?


    Also add some comments to the code describing what the code does and why.
    For example, what does this do?
           i = n%100;
           s = s+i;
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Assignment

    I shouldn't even use the modulo command in there..
    s = s+i; It adds the last number from the loop (value of n) to s (because i!=n). It might be written as s = s+n as well. i = 2+3+4+5+...+n. There is no 1 in i (the loop) because int i = 1. It could be i = 0 (, but professor told us that our application must contain:

    int i,n,s;
            n = //Input;
            i = 1;
            s = 1;
            System.out.print(1);
            while( i!=n ) {
    // WRITE YOUR CODE HERE!
    }
    Last edited by yellowglow; September 3rd, 2014 at 06:19 PM.

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

    Default Re: Loop Assignment

    I still don't see any comments in the code explaining...
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Behaviour of shortcut assignment and normal assignment for float
    By rakeshkr2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2014, 02:54 AM
  2. While loop to perform multiple steps Please help assignment due in 12 hours!!
    By brobertson300 in forum What's Wrong With My Code?
    Replies: 28
    Last Post: March 21st, 2014, 03:17 PM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  4. can someone help me with this while loop assignment
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2010, 10:06 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM