Page 1 of 3 123 LastLast
Results 1 to 20 of 42
  1. #1
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigpips305 View Post
    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. #3
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

    Default 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

  4. #4
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    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 02:12 PM. Reason: wrong word

  5. #5
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

    Default 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. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigpips305 View Post
    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. #7
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

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

  8. #8
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

    Default

    I need the program to print

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

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigpips305 View Post
    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. #10
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

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

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigpips305 View Post
    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 09:14 PM.

  12. #12
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

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

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigpips305 View Post
    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. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    > does NOT equal 2450

    You seem to have included 98 twice as an addend.

  15. #15
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

    Default

    He said the ... were all the way to 98 and he include 2*49

  16. #16
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

    Default

    Forget it, thanks for all your help I just dont get it

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

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

    kind regards,

    Jos

  18. #18
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

    Default 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. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigpips305 View Post
    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. #20
    bigpips305 is offline Member
    Join Date
    Feb 2010
    Posts
    22
    Rep Power
    0

    Default

    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 08:16 AM.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 13
    Last Post: 08-26-2009, 04:08 AM
  2. Jtable including picture
    By JVposter in forum New To Java
    Replies: 3
    Last Post: 02-26-2009, 11:11 PM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  4. Including JAR in applications
    By bugger in forum New To Java
    Replies: 0
    Last Post: 01-11-2008, 09:36 AM
  5. Including HTML in a JLabel
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 09:56 AM

Posting Permissions

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