Results 1 to 6 of 6
  1. #1
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Sum of all even integers? Help?

    The assignment says, "Design and implement and application that reads an integer value and prints the sum of all even integers between 2 and the input value, inclusive. Print an error message if the input value is less than 2. Prompt accordingly." Okay. I don't understand exactly what they want. Do they want the output to be the sum of all the numbers between 2 and the inputted number, so for instance if the user enters 10, the output would be 30 because 2+4+6+8+10 = 30. Or do they want you to have the output list all of the even numbers between two and the inputted number (inclusive), so that the output is 2, 4, 6, 8, 10...? I know you can two the second option with a for loop, but could someone steer me in the right direction on how to do the first option (if that's what it's asking for)? Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Sum of all even integers? Help?

    prints the sum ...
    I think the requirement is the sum.
    how to do the first option
    Define a variable to hold the sum
    write a loop
    add the even numbers to the sum
    end loop
    print the sum
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: Sum of all even integers? Help?

    Nevermind. I got it. A for loop is the best way to do it, right?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Sum of all even integers? Help?

    You are supposed to print "the sum of all even integers" ie 30.

    If you know how to list the even numbers in a for loop, try using the for loop but instead of printing the numbers add them to some variable like total. After the loop finishes total should contain he desired sum.

    [Edit] Yes

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Sum of all even integers? Help?

    I believe they want the first option, print the sum of all event integer. And you can also do it using a for-loop if you want to. Or any other loop statement will be fine.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Sum of all even integers? Help?

    Quote Originally Posted by psx2514 View Post
    A for loop is the best way to do it, right?
    Well, not really because it runs in linear time. But then it wouldn't be much of a programming exercise. Since you are working with integers, you can use the following to check your answer (assuming of course I didn't make a mistake).

    Java Code:
    int n; // initialize to the top range of your series
    int k = n/2;  // the number of even numbers to sum. (k will be the same for n where n = 2r or 2r+1)
    int sum = k * (k + 1); // the sum of even numbers from 2 to n inclusive
    Regards,
    Jim
    Last edited by jim829; 04-03-2013 at 06:50 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. getting integers from a string
    By sehudson in forum New To Java
    Replies: 22
    Last Post: 02-18-2011, 05:34 AM
  2. read integers from txt
    By genji in forum New To Java
    Replies: 12
    Last Post: 05-27-2010, 09:33 PM
  3. Sum of Integers - From File
    By Cubba27 in forum New To Java
    Replies: 8
    Last Post: 03-25-2010, 11:45 AM
  4. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 10:29 AM
  5. Integers and Lists
    By TGH in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 09:49 AM

Tags for this Thread

Posting Permissions

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