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

1. Member
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

2. 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

3. Member
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!

4. Member
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
}```
Otherwise if you just want the final result:
Java 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```
Last edited by Rhez; 02-03-2010 at 03:12 PM. Reason: wrong word

5. Member
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

6. 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

7. Member
Join Date
Feb 2010
Posts
22
Rep Power
0

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; 02-03-2010 at 04:29 PM.

8. Member
Join Date
Feb 2010
Posts
22
Rep Power
0
I need the program to print

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

9. 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".

10. Member
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; 02-03-2010 at 09:00 PM.

11. 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
Last edited by JosAH; 02-03-2010 at 10:14 PM.

12. Member
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; 02-03-2010 at 09:34 PM.

13. 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

14. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
> does NOT equal 2450

You seem to have included 98 twice as an addend.

15. Member
Join Date
Feb 2010
Posts
22
Rep Power
0
He said the ... were all the way to 98 and he include 2*49

16. Member
Join Date
Feb 2010
Posts
22
Rep Power
0
Forget it, thanks for all your help I just dont get it

17. Originally Posted by bigpips305
Forget it, thanks for all your help I just dont get it
You give up too easily.

kind regards,

Jos

18. Member
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

19. 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

20. Member
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 example
Last edited by bigpips305; 02-04-2010 at 09:16 AM.

Page 1 of 3 123 Last

#### Posting Permissions

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