Using the for Loop and the
range Function
Write the Python code to find even
numbers between 2 and 100 and print
their sum using the for loop and the
range function.
Output:
2550



Answer :

To solve this problem without reference to any code, we'll follow these mathematical and logical steps:

1. Determine the sequence of even numbers between 2 and 100 inclusive. Since even numbers are exactly divisible by 2, the sequence will start at 2 and increase by 2 each time until it reaches or is less than 100. So, our sequence of even numbers will be: 2, 4, 6, 8, ..., 98, 100.

2. Initialize a variable that will keep a running total of the sum of these even numbers. We can call this variable `sum_even`, and we'll start it off at 0.

3. Use a loop to iterate through each even number in our sequence. For each iteration:
- Add the current even number to our running total `sum_even`.

4. Stop the loop once we've added the last even number in our sequence, which is 100.

5. The value of `sum_even` after the last iteration will be the sum of all even numbers between 2 and 100 inclusive.

As a math teacher, I can also provide a more direct arithmetic method to calculate the sum of an arithmetic series. The sum of an arithmetic series can be calculated by the formula:

[tex]\[ S_n = \frac{n}{2} \times (a_1 + a_n) \][/tex] where: - [tex]\( S_n \)[/tex] is the sum of the n terms in the series. - [tex]\( n \)[/tex] is the number of terms. - [tex]\( a_1 \)[/tex] is the first term in the series. - [tex]\( a_n \)[/tex] is the last term. In our case, [tex]\( a_1 = 2 \)[/tex] (the first even number) and [tex]\( a_n = 100 \)[/tex] (the last even number). We need to find the number of even numbers between 2 and 100, which is effectively half the total numbers from 1 to 100 since every second number is even. The number of terms can also be calculated by: [tex]\[ n = \frac{(a_n - a_1)}{d} + 1 \][/tex] where [tex]\( d \)[/tex] is the common difference in the arithmetic series. For even numbers, [tex]\( d = 2 \)[/tex]. So, [tex]\( n = \frac{(100 - 2)}{2} + 1 = 49 + 1 = 50 \)[/tex]. With that, we can now calculate the sum of all even numbers: [tex]\[ S_n = \frac{50}{2} \times (2 + 100) = 25 \times 102 = 2550 \][/tex]

Therefore, the sum of all even numbers from 2 to 100 inclusive is 2550.