Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

how do i sum the first 100 even numbers on python?

this is my program

evenNumber = int(input("enter a number: "))

for i in range(2,evenNumber):

evenNumber += 2

print(evenNumber)

im not sure if its right can you help me with it

4 Answers

Relevance
  • Anonymous
    6 years ago
    Favorite Answer

    Create a for loop from 1 to 100, then within the loop, check if the iterated number is even. If even, then add it to the total. If not, then do nothing.

    even_sum = 0

    for i in range(100):

    if (i+1) % 2 > 0:

    even_sum = even_sum + (i+1)

    print even_sum

  • 6 years ago

    This isn't the way you would normally do this, but a clever way of doing this is by recognizing that the sum of the first n even numbers is the same as double the sum of the first n numbers.

    Now, you might wonder how that helps. The sum of [1,2,3,...n] is actually really easy to compute - it's just (n+1)*(n/2). So, if you double that, you just get n*(n+1) as the sum of the first n even numbers.

    So, the sum of the first 100 even numbers would be 100(101), which I can do in my head as 10100.

  • 6 years ago

    The sum of the first `n' multiples of `k' can be expressed as

    Sigma from i = 1 to n of (ki);

    Factor `k' from the summation

    k ((Sigma from i = 1 to n) of i)

    Expand the summation

    Which yields the general formula

    k n (n + 1) (1 / 2)

    So let k = 2 and n = 100

    100 * 101 = 10100

    So in answer to your question:

    print (10100)

    I think you're safe, because I don't think the sum of the first 100 even numbers is going to change, ever.

    Although it might be considered better practice to use a symbolic constant instead of a magic number:

    Sum100Even = 10100

    print (Sum100Even)

    EDIT:

    this is my program

    evenNumber = int(input("enter a number: "))

    for i in range(2,evenNumber):

    .... evenNumber += 2

    print(evenNumber)

    Is a bug. evenNumber is the upper bound on the loop (it's `n' in the above problem). You shouldn't add to the upper bound. Instead, you should start out with a new variable to keep the sum, starting from 0.

    upperBound = int (input ("enter the upper bound"))

    sum = 0

    for i in range (2, upperBound): sum += 2

    print sum

    Now that's almost right --- I changed the name of `evenNumber' to `upperBound', because if you do enter an even number, it will not be included in the generated range.

    That is

    for i in range (2, 4): print i

    would print

    2

    3

    And not 4. Mathematically, the values assigned to i are integers in [lower, upper).

    You don't care about `i' in your loop, so don't give it a name, and use "blank" (`_') as a name instead:

    for _ in range (2, upperBound): sum += 2

    And if you had to do it this way, it would also be considered better style to write just

    print (sum (range (2, 200 + 1, 2)))

  • ?
    Lv 5
    6 years ago

    I will tell you two tricks that I use. First, even numbers

    are numbers of the form 2k, alright? So you might just

    think of i as beginning with 2, and ending with some number

    called 2n.

    A second trick I use is to write the program for the first

    10 even numbers, and see if I can get that right; then if it

    works, I have confidence that the same logic will work for

    100. You see? Get things right for a small group, then go

    for the prize. Good luck.

    >

    > John (gnujohn)

Still have questions? Get your answers by asking now.