45.4k views
5 votes
Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total. Thus your code should associate 11 22 33 ... 4949 50*50 with total. Use no variables other than k and total.

User Kquinn
by
5.5k points

1 Answer

4 votes

Answer:

total = 0

k = 1

while k <= 50:

total += k**2

k += 1

print(total)

Step-by-step explanation:

Note: There some errors in the question. The correct complete question is therefore provided before answering the question as follows:

Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total. Thus your code should associate 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 with total. Use no variables other than k and total.

The answer is now explained as follows:

# Initialize k which is the number of term

k = 1

# Initialize total which is the sum of squares

total = 0

# Loop as long as k is less than or equal to 50.

while (k <= 50):

# Add the square of k to total in order to increase it

total = total + k * k

# Increase the value of k by one.

k += 1

# Finally, output total

print (total)

User Omar Bahir
by
6.3k points