Results 1 to 6 of 6
Thread: Sum of all even integers? Help?
 04032013, 02:59 AM #1Member
 Join Date
 Oct 2012
 Posts
 50
 Rep Power
 0
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!
 04032013, 03:05 AM #2
Re: Sum of all even integers? Help?
prints the sum ...
how to do the first option
write a loop
add the even numbers to the sum
end loop
print the sumIf you don't understand my response, don't ignore it, ask a question.
 04032013, 03:05 AM #3Member
 Join Date
 Oct 2012
 Posts
 50
 Rep Power
 0
Re: Sum of all even integers? Help?
Nevermind. I got it. A for loop is the best way to do it, right?
 04032013, 03:05 AM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
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
 04032013, 03:08 AM #5Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 760
 Rep Power
 8
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 forloop if you want to. Or any other loop statement will be fine.
Website: Learn Java by Examples
 04032013, 06:46 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,618
 Rep Power
 5
Re: Sum of all even integers? Help?
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
JimLast edited by jim829; 04032013 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

getting integers from a string
By sehudson in forum New To JavaReplies: 22Last Post: 02182011, 05:34 AM 
read integers from txt
By genji in forum New To JavaReplies: 12Last Post: 05272010, 09:33 PM 
Sum of Integers  From File
By Cubba27 in forum New To JavaReplies: 8Last Post: 03252010, 11:45 AM 
Set of Integers
By rsjava24 in forum New To JavaReplies: 7Last Post: 01282010, 10:29 AM 
Integers and Lists
By TGH in forum New To JavaReplies: 8Last Post: 01272010, 09:49 AM
Bookmarks