Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Sum of Squares of Even Numbers

Sum squares of even numbers through a limit with Python. Students select terms with a stepped range, square each value, and build the running total.

AdvancedPhase 2 - Level 5: Logical Loop CombinationsExample 11 of 12
sum-of-squares-of-even-numbers.py
1# Take n
2n = int(input("Enter n: "))
3
4# Sum squares of evens
5sum_sq_even = 0
6for i in range(2, n + 1, 2):
7 sum_sq_even += i * i
8
9print(f"Sum of squares of evens: {sum_sq_even}")

Output

Enter n: 10
Sum of squares of evens: 220

In this one

  • Filter through a range step
  • Square selected terms
  • Accumulate without a list

How it works

  1. 1

    Initialize sum_sq_even to zero.

  2. 2

    Visit even values through n and add their squares.

  3. 3

    Print the resulting total.

Edge cases

What happens for n = 1?

What happens for n = 1?

No terms are added, so the total is zero.

Is an odd n included?

Is an odd n included?

No. The range only lands on even values.

What's going on

The range supplies two, four, six, and later even values through n. Each supplied value is squared before being added.

For n equal to six, the total is 4 + 16 + 36 = 56. This differs from squaring the sum of the even values. Starting at two makes a separate parity condition unnecessary and leaves zero for limits below two.

What happens for n = 1? No terms are added, so the total is zero.

Is an odd n included? No. The range only lands on even values.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is added for i = 4?

Pick an answer to check it.

Question 2

What happens for n = 1?

Pick an answer to check it.

Questions people ask

What happens for n = 1?

No terms are added, so the total is zero.

Is an odd n included?

No. The range only lands on even values.

What is added for i = 4?

16.