Sum of Squares

Calculate sum of squares 1² + 2² + ... + n².

Logic BuildingIntermediate
Logic Building
# Take n
n = int(input("Enter n: "))

# Calculate sum of squares
total = 0
for i in range(1, n + 1):
    total += i * i

print(f"Sum of squares: {total}")

Output

Enter n: 5
Sum of squares: 55

Sum squares of numbers from 1 to n.

Key Concepts:

  • Loop from 1 to n
  • Calculate i * i
  • Accumulate sum