Advanced Search

Results 1 to 9 of 9

Thread: Display sum of consecutive numbers

  1. #1
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  2. #2
    Join Date
    Jan 2009
    Location
    Athens, GA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Do you mean the sum of x and y?

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Simply,
    Code:
    public class Numbers {
        public static void main(String[] args) {
            int from = Integer.parseInt(args[0]),
                to   = Integer.parseInt(args[1]);
    
            for (int i = from, total = 0; i < to; ++i)
                System.out.println(x + ": " + (total = total + x));
        }
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Feb 2009
    Location
    pakistan
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile 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 01:55 AM. Reason: signature

  5. #5
    Join Date
    Feb 2009
    Location
    Romania
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by onedollartotown View Post
    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
    you need to be more explicit

  6. #6
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #7
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up 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 );
    }
    }

  8. #8
    Join Date
    Jan 2010
    Location
    India
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    its quite simple...
    public class Numbers
    {
    public static void main(String args[])
    {
    int x = Integer.parseInt(args[0]);
    int y = Integer.parseInt(args[1]);
    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

  9. #9
    Join Date
    Jan 2010
    Location
    India
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    @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

Bookmarks

Posting Permissions

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