11.0k views
2 votes
Counting numbers are the numbers 1,2,3,… (and so on). Assume that n is assigned a positive integer. Write code that uses a while loop to compute the sum of the cubes of the first n counting numbers, and assign this value to total. In other words, total should be assigned: 1∗1∗1+2∗2∗2+…n∗n∗n Use no variables other than n,k, and total. Logic errors. Review the test case table. Remarks and hints - Unexpected identifiers: in, input, print, range - You have to use a while loop in this exercise. - I haven't yet seen a correct solution that uses: ' - I haven't yet seen a correct solution that uses: input - I haven't yet seen a correct solution that uses: int - I haven't yet seen a correct solution that uses: print - Correct solutions that use ( tend to also use , (comma) - We think you might want to consider using: , (comma) - You almost certainly should be using: while

1 Answer

2 votes

Here's a code snippet that utilizes a while loop to compute the sum of the cubes of the first n counting numbers:

n = 1

total = 0

while n <= k:

total += n * n * n

n += 1

How to explain

Initialize variables n and total.

Use a while loop that continues as long as n is less than or equal to the given positive integer k.

Within the loop, increment the total by adding the cube of n (i.e., n * n * n) to the existing total.

Increment n after each iteration to move through the counting numbers up to k.

User SWiggels
by
7.9k points

No related questions found