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

1. 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..

(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 ) {
}```

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

3. Re: Loop Assignment

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

4. Re: Loop Assignment

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

5. 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?  Reply With Quote

6. 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?  Reply With Quote

7. 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;```  Reply With Quote

8. Re: Loop Assignment

This is the output: output.PNG
Is my code so messed up?  Reply With Quote

9. 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;```  Reply With Quote

10. 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 ) {  Reply With Quote
11. Re: Loop Assignment  Reply With Quote