# Sum of all even numbers up to but not including 100

Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last
• 02-03-2010, 11:02 AM
bigpips305
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
• 02-03-2010, 11:54 AM
JosAH
Quote:

Originally Posted by bigpips305
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

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.

kind regards,

Jos
• 02-03-2010, 12:14 PM
bigpips305
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!
• 02-03-2010, 03:08 PM
Rhez
If you mean by print all the results starting from 0 all the way to 98, you'll need a loop:
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 }```
Otherwise if you just want the final result:
Code:

```System.out.print((49/2)*((2*0)+(2*(49-1)))) //formula is S(n)=(n/2)*((2*a)+(d*(n-1)))                                             //where a is the initial number and d is the common difference;                                             //n is the number of terms; prints 2352```
• 02-03-2010, 03:46 PM
bigpips305
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
• 02-03-2010, 03:56 PM
JosAH
Quote:

Originally Posted by bigpips305
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

The first result is because you're printing (0) (0+2) (0+2+4) (0+2+4+6) etc. THe second result is simply incorrect; use the example from the first reply.

kind regards,

Jos
• 02-03-2010, 04:10 PM
bigpips305

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.
• 02-03-2010, 04:30 PM
bigpips305
I need the program to print

2+4+6+8+10+...99 =2450
• 02-03-2010, 05:04 PM
JosAH
Quote:

Originally Posted by bigpips305
I need the program to print

2+4+6+8+10+...99 =2450

Yes we know that but you are ignoring all suggestions; what exactly do you want? The source code of it all? I doubt if anyone will give you that? We are here to help not to do your homework.

kind regards,

Jos

ps. I think you meant "+98" instead of "+99".
• 02-03-2010, 08:46 PM
bigpips305
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??????????
• 02-03-2010, 09:16 PM
JosAH
Quote:

Originally Posted by bigpips305
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??????????

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,

Jos
• 02-03-2010, 09:31 PM
bigpips305
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
• 02-03-2010, 09:38 PM
JosAH
Quote:

Originally Posted by bigpips305
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

Of course not, that prints 2450+98 == 2548. Remove that "+(2*49)" part at the end because the previous term (98) IS (2*49), no need to add that twice.

kind regards,

Jos
• 02-03-2010, 09:39 PM
pbrockway2
> does NOT equal 2450

You seem to have included 98 twice as an addend.
• 02-03-2010, 10:07 PM
bigpips305
He said the ... were all the way to 98 and he include 2*49
• 02-03-2010, 10:08 PM
bigpips305
Forget it, thanks for all your help I just dont get it
• 02-03-2010, 10:09 PM
JosAH
Quote:

Originally Posted by bigpips305
Forget it, thanks for all your help I just dont get it

You give up too easily.

kind regards,

Jos
• 02-03-2010, 10:18 PM
bigpips305
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
• 02-04-2010, 08:17 AM
JosAH
Quote:

Originally Posted by bigpips305
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

I never suggested that; you should read more carefully.

kind regards,

Jos
• 02-04-2010, 09:13 AM
bigpips305
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 example
Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last