Page 1 of 1

## 6 Replies - 31603 Views - Last Post: 24 October 2006 - 07:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=20074&amp;s=1d1f05b8afe1040dcaa5ace0a51c9717&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 wille2511

Reputation: 0
• Posts: 46
• Joined: 24-October 06

Posted 24 October 2006 - 07:30 PM

its like how to add all the looping numbers in the for (loop)
such as
input minimum number is 3
input maximun number is 10

put in the for(loop)
pops out :
a= 3
a= 4
a= 5
...and so on until 10.

the problem is how do i add those numbers together
such as : 2 +3+4+5+6+7+8+9+10 = 52

i really apreciate someone help me out
Is This A Good Question/Topic? 0

## Replies To: adding numbers in For(loops)

Reputation: 0
• Posts: 57
• Joined: 07-August 05

## Re: adding numbers in For(loops)

Posted 24 October 2006 - 07:39 PM

So basically you want to know how to add numbers using a for loop?

You've described the way a for loop works, so I'll assume that you know the syntactic structure for that type of loop.

Can you post some code? For this problem you'd only need a few lines, but you'll need to make sure that you have a variable to keep track of the sum as the loop progresses. Think about that and then maybe post a coding attempt.

### #3 wille2511

Reputation: 0
• Posts: 46
• Joined: 24-October 06

## Re: adding numbers in For(loops)

Posted 24 October 2006 - 07:43 PM

ok ill post the code...though im still finding my problem...you might find some errors:

```import java.util.*;
public class SumFromTo
{
static Scanner input = new Scanner(System.in);

public static void main(String[] args)

{

int min = input.nextInt();

int max = input.nextInt();

int a = 0;

for(int i = min; i <= max; i++)
{

a = a + min;

System.out.println(" min= " +min);
System.out.println(" max = " +max);
System.out.println(" a = " +a);

}

o yea dont forget to add the ending two braketts } }
```

### #4 SPlutard

• code.ninja

Reputation: 18
• Posts: 615
• Joined: 13-August 05

## Re: adding numbers in For(loops)

Posted 24 October 2006 - 07:49 PM

You shoud try changing this line from:
a = a + min;
to
a = a + i;

See if that get you what you're looking for.

In a for loop like this one, the 'min' varible will never get updated - you'll just be adding the same value to itself over and over. (Like 3+3+3+3+3+3....) The 'i' variable is the one that gets incremented and updated.

Reputation: 0
• Posts: 57
• Joined: 07-August 05

## Re: adding numbers in For(loops)

Posted 24 October 2006 - 07:51 PM

Okay, you're close, but a little off. Look at the line:

a = a + min;

Say I enter 5 as min and 7 as max. On the first iteration of the for loop, this line give us a = 5, which is correct. i is then incremented to 6, and we would then expect that on the next run, a = 5 + 6 = 11. But look what you're adding to a. You're still adding min, which in this case is 5, not i. So you're always going to come up with a low answer. Instead of adding min to a, what should you be adding?

ETA: Well, never mind, it looks like SPlutard beat me to it

This post has been edited by dragonlady: 24 October 2006 - 07:52 PM

### #6 SPlutard

• code.ninja

Reputation: 18
• Posts: 615
• Joined: 13-August 05

## Re: adding numbers in For(loops)

Posted 24 October 2006 - 07:54 PM

Ya, but your post is better - I should make people think for themselves more, like you did. Gah - I'm horrible.

### #7 wille2511

Reputation: 0
• Posts: 46
• Joined: 24-October 06

## Re: adding numbers in For(loops)

Posted 24 October 2006 - 07:58 PM

Thx that actually helped =)