CBSE Class 11 Computer Science Question 144 of 161

Flow of Control — Question 21

Back to all questions
21
Question

Question 20b

Write Python programs to sum the given sequences:

12 + 32 + 52 + ..... + n2 (Input n)

Solution
n = int(input("Enter the value of n: "))

i = 1
sum = 0

while i <= n :
    sum += i ** 2
    i += 2

print("Sum =", sum)
Output
Enter the value of n: 9
Sum = 165
Answer