6 Replies - 23912 Views - Last Post: 24 October 2006 - 07:58 PM Rate Topic: -----

#1 wille2511  Icon User is offline

  • New D.I.C Head

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

adding numbers in For(loops)

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)

#2 dragonlady  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 wille2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)

   {

   System.out.println("Please enter the minimum number.");
   int min = input.nextInt();

   System.out.println("Please enter the maximum number.");
   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 } }

Was This Post Helpful? 0
  • +
  • -

#4 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 17
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 dragonlady  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 17
  • View blog
  • 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. :P
Was This Post Helpful? 0
  • +
  • -

#7 wille2511  Icon User is offline

  • New D.I.C Head

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

Re: adding numbers in For(loops)

Posted 24 October 2006 - 07:58 PM

Thx that actually helped =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1