Results 1 to 20 of 42
- 02-03-2010, 10:02 AM #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
- 02-03-2010, 10:54 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 02-03-2010, 11:14 AM #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!
Chad
- 02-03-2010, 02:08 PM #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:
Otherwise if you just want the final result: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*(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 2352Last edited by Rhez; 02-03-2010 at 02:12 PM. Reason: wrong word
- 02-03-2010, 02:46 PM #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
- 02-03-2010, 02:56 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 02-03-2010, 03:10 PM #7
Member
- 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; 02-03-2010 at 03:29 PM.
- 02-03-2010, 03:30 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
- 02-03-2010, 04:04 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 02-03-2010, 07:46 PM #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 08:00 PM.
- 02-03-2010, 08:16 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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; 02-03-2010 at 09:14 PM.
- 02-03-2010, 08:31 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 08:34 PM.
- 02-03-2010, 08:38 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 02-03-2010, 08:39 PM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
> does NOT equal 2450
You seem to have included 98 twice as an addend.
- 02-03-2010, 09:07 PM #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
- 02-03-2010, 09:08 PM #16
Member
- Join Date
- Feb 2010
- Posts
- 22
- Rep Power
- 0
Forget it, thanks for all your help I just dont get it
- 02-03-2010, 09:09 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 02-03-2010, 09:18 PM #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
- 02-04-2010, 07:17 AM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 02-04-2010, 08:13 AM #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 exampleLast edited by bigpips305; 02-04-2010 at 08:16 AM.
Similar Threads
-
netbeans: creating a jar including jar libraries
By satbobo in forum NetBeansReplies: 13Last Post: 08-26-2009, 04:08 AM -
Jtable including picture
By JVposter in forum New To JavaReplies: 3Last Post: 02-26-2009, 11:11 PM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Including JAR in applications
By bugger in forum New To JavaReplies: 0Last Post: 01-11-2008, 09:36 AM -
Including HTML in a JLabel
By Java Tip in forum Java TipReplies: 0Last Post: 11-27-2007, 09:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks