181k views
4 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 1*1 2*2 3*3 ... 49*49 50*50 with total. Use no variables other than k and total.

1 Answer

2 votes

Answer:

total=0

def calcsumnumsquare(k,total):

while k>=1:

total+=int(k)*int(k)

k-=1

return total

print(calcsumnumsquare(4,0))

Step-by-step explanation:

The program required is written above. It uses only two variables k and total as mentioned in the question. And the total is initially set to 9, and then its value is incremented by the square of each k during each loop established by while loop. And finally, when k=1, the output is returned. And we have printed the return value using print.

User Flamingo
by
8.0k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.