Thread: Display sum of consecutive numbers

1. New Comer (less than 5 posts) Join Date
Jan 2009
Posts
3
Thanks
1
Thanked 0 Times in 0 Posts Display sum of consecutive numbers

Hi Guys,

I need your help on while statements.

Currently i have this:-

public class Numbers
{
public static void main(String[] args)
{
int x = Integer.parseInt(args);
int y = Integer.parseInt(args);

while ( x <= y)
{
System.out.println( x );
x = x + 1;
}
}
}

How do I modify the code above to include display of sum of the consecutive numbers? Thanks.

Regards,
John  Reply With Quote

2. Junior Coders Join Date
Jan 2009
Location
Athens, GA
Posts
8
Thanks
0
Thanked 0 Times in 0 Posts Do you mean the sum of x and y?  Reply With Quote

3. Simply,
Code:
public class Numbers {
public static void main(String[] args) {
int from = Integer.parseInt(args),
to   = Integer.parseInt(args);

for (int i = from, total = 0; i < to; ++i)
System.out.println(x + ": " + (total = total + x));
}
}  Reply With Quote

4. New Comer (less than 5 posts) Join Date
Feb 2009
Location
pakistan
Posts
1
Thanks
0
Thanked 0 Times in 0 Posts Display sum of consecutive numbers

The whole concept of the project is so "Given a positive integer Target, in how many different ways can Target be expressed as a sum of consecutive positive integers?"... The user inputs the number in a test box and the result is displayed in a listbox.Example of needed process:
if Target = 63, there are six solutions:
3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11
6 + 7 + 8 + 9 + 10 + 11 + 12
8 + 9 + 10 + 11 + 12 + 13
20 + 21 + 22
31 + 32
63
Last edited by Twey; 02-10-2009 at 02:55 AM. Reason: signature  Reply With Quote

5. Junior Coders Join Date
Feb 2009
Location
Romania
Posts
8
Thanks
0
Thanked 0 Times in 0 Posts  Originally Posted by onedollartotown Hi Guys,

I need your help on while statements.

Currently i have this:-

public class Numbers
{
public static void main(String[] args)
{
int x = Integer.parseInt(args);
int y = Integer.parseInt(args);

while ( x <= y)
{
System.out.println( x );
x = x + 1;
}
}
}

How do I modify the code above to include display of sum of the consecutive numbers? Thanks.

Regards,
John
you need to be more explicit  Reply With Quote

6. Junior Coders Join Date
Mar 2009
Location
Texas
Posts
5
Thanks
0
Thanked 0 Times in 0 Posts Hmm... for x = x + 1; you should just use x++;. It saves time. Other than that... I'm not sure what your Q is.

-♥Angie  Reply With Quote

7. Junior Coders Join Date
Aug 2009
Location
Egypt
Posts
5
Thanks
0
Thanked 0 Times in 0 Posts I think this might be what you want

if you mean you are having a serial of inputs and you want to get their sum;

after taking them from the user in an array

this is the operation:

public static sum (int[] a)
{
int sum=0;
for(int i=0;i<"array size";i++)
{
sum+=a[i];
System.out.println( sum );
}
}  Reply With Quote

8. New Comer (less than 5 posts) Join Date
Jan 2010
Location
India
Posts
2
Thanks
0
Thanked 0 Times in 0 Posts its quite simple...
public class Numbers
{
public static void main(String args[])
{
int x = Integer.parseInt(args);
int y = Integer.parseInt(args);
int sum=0;
while (x<=y)
{
sum+=x;
++x;
}
System.out.println("Sum of the consecutive numbers within "+x +"and" +y +"is: "+sum);
}
}
with regards
Bhargav  Reply With Quote

9. New Comer (less than 5 posts) Join Date
Jan 2010
Location
India
Posts
2
Thanks
0
Thanked 0 Times in 0 Posts @john
the above post is for the sum of all the consecutive numbers...
plss be clear..
do u want the sum of each 2 consecutive numbers or 3 consecutive numbers or...so.on..

thanks
Bhargav  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•