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[0]);
int y = Integer.parseInt(args[1]);
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
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
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 );
}
}