Results 1 to 20 of 42
 02032010, 11:02 AM #1Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
Sum of all even numbers up to but not including 100
First off Hello everyone!! I am new to this forum and Java Programming.
My Structure of Computer Programming instructor has been pretty redundant the first couple of lectures, and then out of no where gave us like 5 assignments, 2 of which are way beyond the scope of what we talked about in class. I did some research online and found some tutorials that list even numbers using the ParseInt method but Im still lost.
I have to write a Java program that sums all the even numbers up to but not including 100. For example, the sum of the even numbers up to but not including 10 is:
2+4+6+8 = 20
 02032010, 11:54 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,728
 Blog Entries
 7
 Rep Power
 21
 02032010, 12:14 PM #3Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
Sum of numbers
Okay Thanks!
I see how that will add one digit and then repmultiply always landing on an even number. I might perhaps still need an example of this written in code for me to grasp. How do I get it to print all the way to <100 and stop? Remember, very new!
Thanks so much!
Chad
 02032010, 03:08 PM #4Member
 Join Date
 Oct 2009
 Posts
 17
 Rep Power
 0
If you mean by print all the results starting from 0 all the way to 98, you'll need a loop:
Java Code:int sum = 0; for(int n = 0; n < 98; n=n+2) //loops 49 times { sum = sum + n; System.out.print(sum + " "); //" " nicely separates each output }
Java Code:System.out.print((49/2)*((2*0)+(2*(491)))) //formula is S(n)=(n/2)*((2*a)+(d*(n1))) //where a is the initial number and d is the common difference; //n is the number of terms; prints 2352
Last edited by Rhez; 02032010 at 03:12 PM. Reason: wrong word
 02032010, 03:46 PM #5Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
thanks
Thanks alot for this, it most deffinetly help me understand the concept.
However, I got two different results with these two different snippets.
It seems as if the first example skipped quite a few of the even numbers:
0 2 6 12 20 30 42 56 72 90 11.....2352 (4,8,10,etc..?)
The second snippet gave me an result of:
2304
 02032010, 03:56 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,728
 Blog Entries
 7
 Rep Power
 21
 02032010, 04:10 PM #7Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
First Reply
Are you referring to your first reply?
I am not sure I totally understand that suggestion.. Was it an example of a bad one and the correct one? You jump from ... to +2 and I am a little confused. I am not sure where you sentence ends, where the code/formula starts.Last edited by bigpips305; 02032010 at 04:29 PM.
 02032010, 04:30 PM #8Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
I need the program to print
2+4+6+8+10+...99 =2450
 02032010, 05:04 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,728
 Blog Entries
 7
 Rep Power
 21
 02032010, 08:46 PM #10Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
I understand
Yes, I understand that thank you, I tried 2 of the 3 suggestions. I don't understand yours at all.
All I am asking is for you to rephrase your sentence. :
There's no need for stupid loops; have a look: the sum of the numbers 1+2+3+ ... +n == n*(n+1)/2. Twice that sum == n*(n+1). You want to find the sum of 2+4+ ... + 2*49 == 49*(49+1) == 49*50 == 2450.
Is this two different cases? I am just not understanding your grammar/sentence structure, appart from the math/code. There is a + after the ... so I not quite sure where the formula starts and where your sentence/example ends.
2+4+ ... + 2*49 == 49*(49+1) == 49*50 == 2450.?
Why do I multiply by 49?? And this doesnt even work> 2+4+ ... +2*49 ==2450??????????Last edited by bigpips305; 02032010 at 09:00 PM.
 02032010, 09:16 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,728
 Blog Entries
 7
 Rep Power
 21
The ellipses (...) are a (more or less) official notation for a continuation, so the sum of all even numbers starting at 2 and ending at 98 is written as:
2+4+6+ ... +(2*49).
The sum of the first 'n' numbers (this is the general case) is written as:
1+2+3+ ... +n.
Centuries (milleniums actually) ago it was discovered that this sum equals:
n*(n+1)/2
Try some small examples. So if the sum of the first 'n' numbers can be written as n*(n+1)/2 then twice this sum can be written as n*(n+1).
So, the sum of the first even numbers up to 98 is:
2+4+6+8+ ... +2*49 == 49*(49+1) == 49*50 == 2450.
Comprendo?
kind regards,
JosLast edited by JosAH; 02032010 at 10:14 PM.
 02032010, 09:31 PM #12Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
Detailed
2+4+6+8+10+12+14+16+18+20+22+24+26+28+30+32+34+36+ 38+40+42+44+46+48+50+52+54+56+58+60+62+64+66+68+70 +72+74+76+78+80+82+84+86+88+90+92+94+96+98+(2*49)) does NOT equal 2450
Last edited by bigpips305; 02032010 at 09:34 PM.
 02032010, 09:38 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,728
 Blog Entries
 7
 Rep Power
 21
 02032010, 09:39 PM #14Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,573
 Rep Power
 12
> does NOT equal 2450
You seem to have included 98 twice as an addend.
 02032010, 10:07 PM #15Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
He said the ... were all the way to 98 and he include 2*49
 02032010, 10:08 PM #16Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
Forget it, thanks for all your help I just dont get it
 02032010, 10:09 PM #17
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,728
 Blog Entries
 7
 Rep Power
 21
 02032010, 10:18 PM #18Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
Time
I've got 8 hours into already, even my instructor would give me the first little bit, I replaced the n=n+1 from on second suggestion with your n=n*n+1 but when I run it it acts like it's calculating forever and doesn't display anything. This is just a freaking practice assignment, not even worth a grade. I can't get one line of freaking code?? The irony that u probably use abstraction all the time. Stop using libraries and other people hardworking, reinvent the wheel each time
 02042010, 08:17 AM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,728
 Blog Entries
 7
 Rep Power
 21
 02042010, 09:13 AM #20Member
 Join Date
 Feb 2010
 Posts
 22
 Rep Power
 0
2+4+6+8+ ... +2*49 == 49*(49+1) == 49*50 == 2450.
This is usless to me; never in all 3 of my text books, one on java, one on c++ and one on introduction to programming does any example using a elipse give an explanation like this. I just dont understand what your saying. Period.
When I look at that, i want to just type this in.
System.out.println(49*50) based on this exampleLast edited by bigpips305; 02042010 at 09:16 AM.
Similar Threads

netbeans: creating a jar including jar libraries
By satbobo in forum NetBeansReplies: 13Last Post: 08262009, 05:08 AM 
Jtable including picture
By JVposter in forum New To JavaReplies: 3Last Post: 02272009, 12:11 AM 
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10152008, 12:46 AM 
Including JAR in applications
By bugger in forum New To JavaReplies: 0Last Post: 01112008, 10:36 AM 
Including HTML in a JLabel
By Java Tip in forum Java TipReplies: 0Last Post: 11272007, 10:56 AM
Bookmarks